#include <bits/stdc++.h>
using namespace std;
int64_t a[300005], aux[300005];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64_t t;
  cin >> t;
  while (t--) {
    int64_t n, q;
    int64_t sum = 0;
    cin >> n >> q;
    for (int64_t i = 1; i <= n; i++) {
      cin >> a[i];
      aux[i] = a[i] - a[i - 1];
      if (aux[i] > 0) sum += aux[i];
    }
    cout << sum << '\n';
    int64_t l, r;
    for (int64_t i = 1; i <= q; i++) {
      cin >> l >> r;
      set<int64_t> rm = {l - 1, l, l + 1, r - 1, r, r + 1};
      for (auto el : rm)
        if (el >= 0 && el <= n && aux[el] > 0) sum -= aux[el];
      swap(a[l], a[r]);
      for (auto el : rm) {
        if (el) aux[el] = a[el] - a[el - 1];
        if (el >= 0 && el <= n && aux[el] > 0) sum += aux[el];
      }
      cout << sum << '\n';
    }
  }
  return 0;
}
