#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, q;
    cin >> n >> q;
    vector<long long> a(n + 2);
    for (int i = 1; i <= n; i++) cin >> a[i];
    long long sum = 0;
    vector<long long> b(n + 1);
    for (int i = 0; i <= n; i++) {
      b[i] = a[i + 1] - a[i];
      if (b[i] < 0) sum += b[i];
    }
    function<void(int, long long)> change = [&](int i, long long v) {
      a[i] = v;
      long long v0 = a[i] - a[i - 1];
      if (b[i - 1] < 0) sum -= b[i - 1];
      b[i - 1] = v0;
      if (b[i - 1] < 0) sum += b[i - 1];
      long long v1 = a[i + 1] - a[i];
      if (b[i] < 0) sum -= b[i];
      b[i] = v1;
      if (b[i] < 0) sum += b[i];
    };
    cout << -sum << "\n";
    for (int i = 0; i < q; i++) {
      int le, ri;
      cin >> le >> ri;
      long long tt = a[le];
      change(le, a[ri]);
      change(ri, tt);
      cout << -sum << "\n";
    }
  }
  return 0;
}
