#include <bits/stdc++.h>
using namespace std;
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) {
  if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
  if (x > y) x = y;
}
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < 48) c = getchar();
  while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
  return x;
}
const int maxn = 3e5 + 10;
int n, q, A[maxn], chk[maxn], val[maxn];
bool pd1(int x) { return A[x - 1] < A[x] && A[x] > A[x + 1]; }
bool pd2(int x) { return A[x - 1] > A[x] && A[x] < A[x + 1]; }
void solve() {
  cin >> n >> q;
  for (int i = (1), iend = (n); i <= iend; ++i) A[i] = read();
  A[n + 1] = 0;
  long long ans = 0;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    chk[i] = 0;
    if (pd1(i)) chk[i] = 1;
    if (pd2(i)) chk[i] = -1;
    ans += val[i] = A[i] * chk[i];
  }
  cout << ans << endl;
  while (q--) {
    int x = read(), y = read();
    swap(A[x], A[y]);
    for (int t = (-1), tend = (1); t <= tend; ++t) {
      int i = x + t;
      for (int j = (0), jend = (1); j <= jend; ++j) {
        if (i >= 1 && i <= n) {
          ans -= val[i];
          chk[i] = 0;
          if (pd1(i)) chk[i] = 1;
          if (pd2(i)) chk[i] = -1;
          ans += val[i] = A[i] * chk[i];
        }
        i = y + t;
      }
    }
    printf("%lld\n", ans);
  }
}
signed main() {
  int T = read();
  while (T--) solve();
  return 0;
}
