#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 5;
long long n, q, a[MAXN], ans = 0;
void insert(long long i) {
  if (i == 0 || i == n + 1) {
    return;
  }
  if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
    ans += a[i];
  }
  if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
    ans -= a[i];
  }
}
void erase(long long i) {
  if (i == 0 || i == n + 1) {
    return;
  }
  if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
    ans -= a[i];
  }
  if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
    ans += a[i];
  }
}
signed main(void) {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long tests;
  cin >> tests;
  while (tests--) {
    cin >> n >> q;
    for (long long i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    a[0] = a[n + 1] = -1;
    ans = 0;
    for (long long i = 1; i <= n; ++i) {
      insert(i);
    }
    cout << ans << "\n";
    for (long long i = 0; i < q; ++i) {
      long long l, r;
      cin >> l >> r;
      erase(l - 1), erase(l), erase(l + 1);
      if (l != r) {
        if (r - l > 2) {
          erase(r - 1);
        }
        if (r - l > 1) {
          erase(r);
        }
        erase(r + 1);
      }
      swap(a[l], a[r]);
      insert(l - 1), insert(l), insert(l + 1);
      if (l != r) {
        if (r - l > 2) {
          insert(r - 1);
        }
        if (r - l > 1) {
          insert(r);
        }
        insert(r + 1);
      }
      cout << ans << "\n";
    }
  }
}
