#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7;
int v[N], n;
bool add[N], sub[N];
long long ans;
void query(int i) {
  if (add[i] || sub[i]) return;
  if (i > n || !i) return;
  if (i != n) {
    if (v[i] > v[i - 1] && v[i + 1] < v[i]) add[i] = 1, ans += v[i];
    if (v[i - 1] > v[i] && v[i + 1] > v[i]) sub[i] = 1, ans -= v[i];
  } else if (v[i] > v[i - 1])
    add[i] = 1, ans += v[i];
}
void del(int i) {
  if (i > n || !i) return;
  if (sub[i]) ans += v[i];
  if (add[i]) ans -= v[i];
  sub[i] = add[i] = 0;
}
int main() {
  int tc = 1;
  scanf("%d", &tc);
  for (int cn = 1; cn <= tc; cn++) {
    int q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%d", &v[i]), sub[i] = add[i] = 0;
    for (int i = 1; i <= n; i++) query(i);
    printf("%lld\n", ans);
    while (q--) {
      int l, r;
      scanf("%d%d", &l, &r);
      del(l - 1), del(l), del(l + 1), del(r - 1), del(r), del(r + 1);
      swap(v[l], v[r]);
      query(l - 1), query(l), query(l + 1), query(r - 1), query(r),
          query(r + 1);
      printf("%lld\n", ans);
    }
    ans = 0;
  }
  return 0;
}
