#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10;
long long N, Q, T, val[MAXN], f[MAXN][2];
int main() {
  ios::sync_with_stdio(false);
  cin >> T;
  while (T--) {
    cin >> N >> Q;
    long long ans = 0;
    for (long long i = 1; i <= N; i++) cin >> val[i];
    for (long long i = 1; i < N; i++) ans += max(val[i] - val[i + 1], 0LL);
    cout << ans + val[N] << endl;
    for (long long i = 1, x, y; i <= Q; i++) {
      cin >> x >> y;
      if (x == y) {
        cout << ans + val[N] << endl;
        continue;
      }
      if (x - 1) ans -= max(val[x - 1] - val[x], 0LL);
      if (x < N) ans -= max(val[x] - val[x + 1], 0LL);
      if (y - 1 && y - 1 != x) ans -= max(val[y - 1] - val[y], 0LL);
      if (y < N) ans -= max(val[y] - val[y + 1], 0LL);
      swap(val[x], val[y]);
      if (x - 1) ans += max(val[x - 1] - val[x], 0LL);
      if (x < N) ans += max(val[x] - val[x + 1], 0LL);
      if (y - 1 && y - 1 != x) ans += max(val[y - 1] - val[y], 0LL);
      if (y < N) ans += max(val[y] - val[y + 1], 0LL);
      cout << ans + val[N] << endl;
    }
  }
  return 0;
}
