#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<cctype> #define ll long long #define db double #define maxn 300010 #define cn const node #define cQ const Queue #define gc getchar usingnamespacestd;
intread(){ int x = 0; char c = gc(); while (!isdigit(c)) c = gc(); while (isdigit(c)) x = x * 10 + c - '0', c = gc(); return x; }
int n, m, a[maxn];
ll ans; inlinevoiddel(int k){ if (a[k] > a[k + 1]) ans -= a[k] - a[k + 1]; if (a[k - 1] > a[k]) ans -= a[k - 1] - a[k]; }
inlinevoidadd(int k){ if (a[k] > a[k + 1]) ans += a[k] - a[k + 1]; if (a[k - 1] > a[k]) ans += a[k - 1] - a[k]; }
voidwork(){ cin >> n >> m; ans = 0; a[0] = a[n + 1] = 0; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 0; i <= n; ++i) if (a[i] > a[i + 1]) ans += a[i] - a[i + 1]; cout << ans << endl; for (int i = 1; i <= m; ++i) { int l = read(), r = read(); del(l); del(r); if (l == r - 1 && a[l] > a[r]) ans += a[l] - a[r]; swap(a[l], a[r]); add(l); add(r); if (l == r - 1 && a[l] > a[r]) ans -= a[l] - a[r]; cout << ans << endl; } }
intmain(){ int T; cin >> T; while (T--) work(); return0; }