#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, q;
int a[300030];
int b[300030];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &q);
    memset(b, 0, sizeof(b));
    memset(a, 0, sizeof(a));
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      b[i] = a[i] - a[i - 1];
      if (b[i] > 0) sum += b[i];
    }
    printf("%lld\n", sum);
    for (int i = 1; i <= q; i++) {
      int l, r;
      scanf("%d%d", &l, &r);
      long long c = a[l];
      a[l] = a[r];
      a[r] = c;
      if (b[l] > 0) sum -= b[l];
      if (b[l + 1] > 0) sum -= b[l + 1];
      b[l] = a[l] - a[l - 1];
      b[l + 1] = a[l + 1] - a[l];
      if (b[l] > 0) sum += b[l];
      if (b[l + 1] > 0) sum += b[l + 1];
      if (b[r] > 0) sum -= b[r];
      if (b[r + 1] > 0) sum -= b[r + 1];
      b[r] = a[r] - a[r - 1];
      b[r + 1] = a[r + 1] - a[r];
      if (b[r] > 0) sum += b[r];
      if (b[r + 1] > 0) sum += b[r + 1];
      printf("%lld\n", sum);
    }
  }
  return 0;
}
