#include <bits/stdc++.h>
const long long MaxN = 3e5 + 10;
long long n, q, a[MaxN], b[MaxN];
long long read() {
  long long x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while ('0' <= ch && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}
signed main() {
  long long T = read();
  while (T--) {
    long long l, r, ans = 0;
    n = read(), q = read();
    for (long long i = 1; i <= n; i++) a[i] = read(), b[i] = a[i] - a[i - 1];
    for (long long i = 1; i <= n; i++)
      if (b[i] > 0) ans += b[i];
    printf("%lld\n", ans);
    while (q--) {
      l = read(), r = read(), std::swap(a[l], a[r]);
      ans -= std::max(b[l], 0ll), b[l] = a[l] - a[l - 1],
                                  ans += std::max(b[l], 0ll);
      if (l < n)
        ans -= std::max(b[l + 1], 0ll), b[l + 1] = a[l + 1] - a[l],
                                              ans += std::max(b[l + 1], 0ll);
      ans -= std::max(b[r], 0ll), b[r] = a[r] - a[r - 1],
                                  ans += std::max(b[r], 0ll);
      if (r < n)
        ans -= std::max(b[r + 1], 0ll), b[r + 1] = a[r + 1] - a[r],
                                              ans += std::max(b[r + 1], 0ll);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
