#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
long long a[maxn];
int main() {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    memset(a, 0, sizeof(a));
    long long n, q;
    cin >> n >> q;
    long long res = 0;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    for (long long i = 1; i <= n; i++) {
      if (a[i] - a[i + 1] > 0) {
        res += (a[i] - a[i + 1]);
      }
    }
    cout << res << endl;
    while (q--) {
      long long l, r;
      cin >> l >> r;
      if (a[l - 1] - a[l] > 0) res -= (a[l - 1] - a[l]);
      if (a[r - 1] - a[r] > 0 && l != r - 1) res -= (a[r - 1] - a[r]);
      if (a[l] - a[l + 1] > 0) res -= (a[l] - a[l + 1]);
      if (a[r] - a[r + 1] > 0) res -= (a[r] - a[r + 1]);
      swap(a[l], a[r]);
      if (a[l - 1] - a[l] > 0) res += (a[l - 1] - a[l]);
      if (a[r - 1] - a[r] > 0 && l != r - 1) res += (a[r - 1] - a[r]);
      if (a[l] - a[l + 1] > 0) res += (a[l] - a[l + 1]);
      if (a[r] - a[r + 1] > 0) res += (a[r] - a[r + 1]);
      cout << res << endl;
    }
  }
}
