#include <bits/stdc++.h>
constexpr auto Inf = 0X3F3F3F3F;
using namespace std;
namespace IO {
inline long long read() {
  long long o = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c > '/' && c < ':') {
    o = o * 10 + c - '0';
    c = getchar();
  }
  return o * f;
}
inline char recd() {
  char o;
  while ((o = getchar()) < '0' || o > '9')
    ;
  return o;
}
inline double reod() {
  double o = read(), f = 1;
  char c;
  while ((c = getchar()) > '/' && c < ':') o += (c - '0') / (f *= 10);
  return o;
}
}  // namespace IO
using namespace IO;
const int SIZE = 3E5 + 7, Mod = (1E9 + 7, 998244353);
long long w[SIZE], a[SIZE];
int main() {
  int T = read();
  while (T--) {
    int N = read(), M = read();
    long long ans = 0;
    for (int pos = 1; pos <= N; pos++)
      w[pos] = read(), a[pos] = w[pos] - w[pos - 1], ans += max(a[pos], 0LL);
    a[N + 1] = 0;
    printf("%lld\n", ans);
    while (M--) {
      int L = read(), R = read();
      ans -= max(a[L], 0LL), ans -= max(a[L + 1], 0LL);
      ans -= max(a[R + 1], 0LL);
      if (L != R - 1) ans -= max(a[R], 0LL);
      swap(w[L], w[R]);
      a[L] = w[L] - w[L - 1], a[R] = w[R] - w[R - 1];
      if (L != N) a[L + 1] = w[L + 1] - w[L];
      if (R != N) a[R + 1] = w[R + 1] - w[R];
      ans += max(a[L], 0LL), ans += max(a[L + 1], 0LL);
      ans += max(a[R + 1], 0LL);
      if (L != R - 1) ans += max(a[R], 0LL);
      printf("%lld\n", ans);
    }
  }
}
