#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int a[N];
int flag[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, q;
    scanf("%d%d", &n, &q);
    a[0] = a[n + 1] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
        ans += a[i];
        flag[i] = 1;
      } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
        ans -= a[i];
        flag[i] = -1;
      } else
        flag[i] = 0;
    }
    printf("%lld\n", ans);
    while (q--) {
      int l, r;
      scanf("%d%d", &l, &r);
      swap(a[l], a[r]);
      for (int i = l - 1; i <= l + 1; i++) {
        if (i == 0 || i == n + 1) continue;
        if (flag[i] == 1) {
          if (i == l)
            ans -= a[r];
          else if (i == r)
            ans -= a[l];
          else
            ans -= a[i];
        }
        if (flag[i] == -1) {
          if (i == l)
            ans += a[r];
          else if (i == r)
            ans += a[l];
          else
            ans += a[i];
        }
        if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
          ans += a[i];
          flag[i] = 1;
        } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
          ans -= a[i];
          flag[i] = -1;
        } else
          flag[i] = 0;
      }
      for (int i = r - 1; i <= r + 1; i++) {
        if (i == 0 || i == n + 1 || i == l - 1 || i == l || i == l + 1)
          continue;
        if (flag[i] == 1) {
          if (i == l)
            ans -= a[r];
          else if (i == r)
            ans -= a[l];
          else
            ans -= a[i];
        }
        if (flag[i] == -1) {
          if (i == l)
            ans += a[r];
          else if (i == r)
            ans += a[l];
          else
            ans += a[i];
        }
        if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
          ans += a[i];
          flag[i] = 1;
        } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
          ans -= a[i];
          flag[i] = -1;
        } else
          flag[i] = 0;
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
