#include <bits/stdc++.h>
using namespace std;
using L = long long;
using PII = pair<int, int>;
using PLL = pair<L, L>;
using VI = vector<int>;
using VL = vector<L>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using VPII = vector<PII>;
using VPLL = vector<PLL>;
using UMII = unordered_map<int, int>;
using UMLL = unordered_map<L, L>;
using USI = unordered_set<int>;
using USL = unordered_set<L>;
void SC(int &i) { scanf("%d", &i); }
void SC(int &i, int &j) { scanf("%d %d", &i, &j); }
void SC(int &i, int &j, int &k) { scanf("%d %d %d", &i, &j, &k); }
void SC(L &i) { scanf("%lld", &i); }
void SC(L &i, L &j) { scanf("%lld %lld", &i, &j); }
void SC(L &i, L &j, L &k) { scanf("%lld %lld %lld", &i, &j, &k); }
void print(VL arr, const string sep = " ", const string end = "\n") {
  for (L i = 0; i < ((int)arr.size()) - 1; ++i)
    printf("%lld%s", arr[i], sep.c_str());
  printf("%lld", arr.back());
  printf("%s", end.c_str());
}
void print(VI arr, const string sep = " ", const string end = "\n") {
  for (L i = 0; i < ((int)arr.size()) - 1; ++i)
    printf("%d%s", arr[i], sep.c_str());
  printf("%d", arr.back());
  printf("%s", end.c_str());
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int MAX = 3e5 + 7;
L arr[MAX], n, ans, q;
void add(L i, L mul) {
  if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]) ans += mul * arr[i];
  if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]) ans -= mul * arr[i];
}
void makeset(set<L> &curset, L ind) {
  for (L i : {ind - 1, ind, ind + 1}) {
    if (i <= 0 or i > n) continue;
    curset.insert(i);
  }
}
void solve() {
  SC(n, q);
  ans = 0, arr[0] = arr[n + 1] = -INF;
  for (L i = 1; i <= n; ++i) SC(arr[i]);
  for (L i = 1; i <= n; ++i) add(i, 1);
  print(VL{ans});
  for (L qi = 0; qi < q; ++qi) {
    L l, r;
    SC(l, r);
    set<L> curset;
    makeset(curset, l);
    makeset(curset, r);
    for (L cur : curset) add(cur, -1);
    swap(arr[l], arr[r]);
    for (L cur : curset) add(cur, 1);
    print(VL{ans});
  }
}
int main() {
  int T;
  SC(T);
  while (T--) {
    solve();
  }
  return 0;
}
