#include <bits/stdc++.h>
using namespace std;
class SegmentTree {
public:
int n;
vector<int> maxTree, minTree;
SegmentTree(int size) {
n = size;
maxTree.resize(4 * n, INT_MIN);
minTree.resize(4 * n, INT_MAX);
}
void build(vector<int>& a, int v, int tl, int tr) {
if (tl == tr) {
maxTree[v] = a[tl];
minTree[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v * 2, tl, tm);
build(a, v * 2 + 1, tm + 1, tr);
maxTree[v] = max(maxTree[v * 2], maxTree[v * 2 + 1]);
minTree[v] = min(minTree[v * 2], minTree[v * 2 + 1]);
}
}
void update(int v, int tl, int tr, int pos, int newVal) {
if (tl == tr) {
maxTree[v] = newVal;
minTree[v] = newVal;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm) {
update(v * 2, tl, tm, pos, newVal);
} else {
update(v * 2 + 1, tm + 1, tr, pos, newVal);
}
maxTree[v] = max(maxTree[v * 2], maxTree[v * 2 + 1]);
minTree[v] = min(minTree[v * 2], minTree[v * 2 + 1]);
}
}
pair<int, int> query(int v, int tl, int tr, int l, int r) {
if (l > r) return {INT_MIN, INT_MAX};
if (l == tl && r == tr) {
return {maxTree[v], minTree[v]};
}
int tm = (tl + tr) / 2;
auto left = query(v * 2, tl, tm, l, min(r, tm));
auto right = query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return {max(left.first, right.first), min(left.second, right.second)};
}
};
void solve() {
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
SegmentTree segTree(n);
segTree.build(a, 1, 0, n - 1);
auto getMaxConvenience = [&]() -> int {
int maxConvenience = INT_MIN;
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {
auto [mx, mn] = segTree.query(1, 0, n - 1, l, r);
int convenience = mx - mn - (r - l);
maxConvenience = max(maxConvenience, convenience);
}
}
return maxConvenience;
};
cout << getMaxConvenience() << "\n";
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
segTree.update(1, 0, n - 1, p - 1, x);
cout << getMaxConvenience() << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBTZWdtZW50VHJlZSB7CnB1YmxpYzoKICAgIGludCBuOwogICAgdmVjdG9yPGludD4gbWF4VHJlZSwgbWluVHJlZTsKCiAgICBTZWdtZW50VHJlZShpbnQgc2l6ZSkgewogICAgICAgIG4gPSBzaXplOwogICAgICAgIG1heFRyZWUucmVzaXplKDQgKiBuLCBJTlRfTUlOKTsKICAgICAgICBtaW5UcmVlLnJlc2l6ZSg0ICogbiwgSU5UX01BWCk7CiAgICB9CgogICAgdm9pZCBidWlsZCh2ZWN0b3I8aW50PiYgYSwgaW50IHYsIGludCB0bCwgaW50IHRyKSB7CiAgICAgICAgaWYgKHRsID09IHRyKSB7CiAgICAgICAgICAgIG1heFRyZWVbdl0gPSBhW3RsXTsKICAgICAgICAgICAgbWluVHJlZVt2XSA9IGFbdGxdOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGludCB0bSA9ICh0bCArIHRyKSAvIDI7CiAgICAgICAgICAgIGJ1aWxkKGEsIHYgKiAyLCB0bCwgdG0pOwogICAgICAgICAgICBidWlsZChhLCB2ICogMiArIDEsIHRtICsgMSwgdHIpOwogICAgICAgICAgICBtYXhUcmVlW3ZdID0gbWF4KG1heFRyZWVbdiAqIDJdLCBtYXhUcmVlW3YgKiAyICsgMV0pOwogICAgICAgICAgICBtaW5UcmVlW3ZdID0gbWluKG1pblRyZWVbdiAqIDJdLCBtaW5UcmVlW3YgKiAyICsgMV0pOwogICAgICAgIH0KICAgIH0KCiAgICB2b2lkIHVwZGF0ZShpbnQgdiwgaW50IHRsLCBpbnQgdHIsIGludCBwb3MsIGludCBuZXdWYWwpIHsKICAgICAgICBpZiAodGwgPT0gdHIpIHsKICAgICAgICAgICAgbWF4VHJlZVt2XSA9IG5ld1ZhbDsKICAgICAgICAgICAgbWluVHJlZVt2XSA9IG5ld1ZhbDsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBpbnQgdG0gPSAodGwgKyB0cikgLyAyOwogICAgICAgICAgICBpZiAocG9zIDw9IHRtKSB7CiAgICAgICAgICAgICAgICB1cGRhdGUodiAqIDIsIHRsLCB0bSwgcG9zLCBuZXdWYWwpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdXBkYXRlKHYgKiAyICsgMSwgdG0gKyAxLCB0ciwgcG9zLCBuZXdWYWwpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG1heFRyZWVbdl0gPSBtYXgobWF4VHJlZVt2ICogMl0sIG1heFRyZWVbdiAqIDIgKyAxXSk7CiAgICAgICAgICAgIG1pblRyZWVbdl0gPSBtaW4obWluVHJlZVt2ICogMl0sIG1pblRyZWVbdiAqIDIgKyAxXSk7CiAgICAgICAgfQogICAgfQoKICAgIHBhaXI8aW50LCBpbnQ+IHF1ZXJ5KGludCB2LCBpbnQgdGwsIGludCB0ciwgaW50IGwsIGludCByKSB7CiAgICAgICAgaWYgKGwgPiByKSByZXR1cm4ge0lOVF9NSU4sIElOVF9NQVh9OwogICAgICAgIGlmIChsID09IHRsICYmIHIgPT0gdHIpIHsKICAgICAgICAgICAgcmV0dXJuIHttYXhUcmVlW3ZdLCBtaW5UcmVlW3ZdfTsKICAgICAgICB9CiAgICAgICAgaW50IHRtID0gKHRsICsgdHIpIC8gMjsKICAgICAgICBhdXRvIGxlZnQgPSBxdWVyeSh2ICogMiwgdGwsIHRtLCBsLCBtaW4ociwgdG0pKTsKICAgICAgICBhdXRvIHJpZ2h0ID0gcXVlcnkodiAqIDIgKyAxLCB0bSArIDEsIHRyLCBtYXgobCwgdG0gKyAxKSwgcik7CiAgICAgICAgcmV0dXJuIHttYXgobGVmdC5maXJzdCwgcmlnaHQuZmlyc3QpLCBtaW4obGVmdC5zZWNvbmQsIHJpZ2h0LnNlY29uZCl9OwogICAgfQp9OwoKdm9pZCBzb2x2ZSgpIHsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG4sIHE7CiAgICAgICAgY2luID4+IG4gPj4gcTsKICAgICAgICB2ZWN0b3I8aW50PiBhKG4pOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGNpbiA+PiBhW2ldOwogICAgICAgIH0KCiAgICAgICAgU2VnbWVudFRyZWUgc2VnVHJlZShuKTsKICAgICAgICBzZWdUcmVlLmJ1aWxkKGEsIDEsIDAsIG4gLSAxKTsKCiAgICAgICAgYXV0byBnZXRNYXhDb252ZW5pZW5jZSA9IFsmXSgpIC0+IGludCB7CiAgICAgICAgICAgIGludCBtYXhDb252ZW5pZW5jZSA9IElOVF9NSU47CiAgICAgICAgICAgIGZvciAoaW50IGwgPSAwOyBsIDwgbjsgbCsrKSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCByID0gbDsgciA8IG47IHIrKykgewogICAgICAgICAgICAgICAgICAgIGF1dG8gW214LCBtbl0gPSBzZWdUcmVlLnF1ZXJ5KDEsIDAsIG4gLSAxLCBsLCByKTsKICAgICAgICAgICAgICAgICAgICBpbnQgY29udmVuaWVuY2UgPSBteCAtIG1uIC0gKHIgLSBsKTsKICAgICAgICAgICAgICAgICAgICBtYXhDb252ZW5pZW5jZSA9IG1heChtYXhDb252ZW5pZW5jZSwgY29udmVuaWVuY2UpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBtYXhDb252ZW5pZW5jZTsKICAgICAgICB9OwoKICAgICAgICBjb3V0IDw8IGdldE1heENvbnZlbmllbmNlKCkgPDwgIlxuIjsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBxOyBpKyspIHsKICAgICAgICAgICAgaW50IHAsIHg7CiAgICAgICAgICAgIGNpbiA+PiBwID4+IHg7CiAgICAgICAgICAgIHNlZ1RyZWUudXBkYXRlKDEsIDAsIG4gLSAxLCBwIC0gMSwgeCk7CiAgICAgICAgICAgIGNvdXQgPDwgZ2V0TWF4Q29udmVuaWVuY2UoKSA8PCAiXG4iOwogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cg==