#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
const int N = 1e6 + 10;
int t, n, q;
int l, r;
int a[N], tag[N];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n), scanf("%d", &q);
    int mmin = n, mmax = 0;
    a[0] = a[n + 1] = 0;
    for (register int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      mmax = max(mmax, a[i]);
    }
    long long res = 0;
    for (register int i = 1; i <= n; ++i) {
      if (a[i] > a[i - 1] && a[i] > a[i + 1]) res += a[i], tag[i] = 1;
      if (a[i] < a[i - 1] && a[i] < a[i + 1]) res -= a[i], tag[i] = -1;
    }
    cout << res << endl;
    while (q--) {
      scanf("%d", &l), scanf("%d", &r);
      swap(a[l], a[r]);
      swap(tag[l], tag[r]);
      for (register int i = max(1, l - 1); i <= min(l + 1, n); ++i) {
        if (tag[i] != 0) {
          res -= tag[i] * a[i];
          tag[i] = 0;
        }
        if (a[i] < a[i - 1] && a[i] < a[i + 1]) res -= a[i], tag[i] = -1;
        if (a[i] > a[i - 1] && a[i] > a[i + 1]) res += a[i], tag[i] = 1;
      }
      for (register int i = max(1, r - 1); i <= min(r + 1, n); ++i) {
        if (tag[i] != 0) {
          res -= tag[i] * a[i];
          tag[i] = 0;
        }
        if (a[i] < a[i - 1] && a[i] < a[i + 1]) res -= a[i], tag[i] = -1;
        if (a[i] > a[i - 1] && a[i] > a[i + 1]) res += a[i], tag[i] = 1;
      }
      cout << res << endl;
    }
    for (register int i = 1; i <= n; ++i) tag[i] = 0;
  }
  return 0;
}
