#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 3e5 + 100;
int a[N], n;
long long ans;
void update(int i, int c) {
  if (i <= 0 || i > n) return;
  if (a[i] > a[i - 1] && a[i] > a[i + 1]) ans += a[i] * c;
  if (a[i] < a[i - 1] && a[i] < a[i + 1]) ans -= a[i] * c;
}
int main() {
  int w;
  cin >> w;
  while (w--) {
    int q;
    scanf("%d%d", &n, &q);
    a[0] = a[n + 1] = 0;
    for (int i = 1; i <= n; i++) scanf("%d", a + i);
    ans = 0;
    for (int i = 1; i <= n; i++) update(i, 1);
    printf("%lld\n", ans);
    while (q--) {
      int l, r;
      scanf("%d%d", &l, &r);
      set<int> st;
      for (int i = -1; i <= 1; i++) {
        st.insert(l + i);
        st.insert(r + i);
      }
      for (int i : st) update(i, -1);
      swap(a[l], a[r]);
      for (int i : st) update(i, 1);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
