#include <bits/stdc++.h>
using namespace std;
long long insert(long long i, vector<long long> a) {
  long long ans = 0;
  if (i == 0 || i == a.size() - 1) return 0;
  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];
  return ans;
}
long long erase(long long i, vector<long long> a) {
  long long ans = 0;
  if (i == 0 || i == a.size() - 1) return 0;
  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];
  return ans;
}
signed main() {
  ios_base::sync_with_stdio();
  cin.tie();
  cout.tie();
  long long t;
  cin >> t;
  for (long long t123 = 0; t123 < t; ++t123) {
    long long n, q;
    cin >> n >> q;
    vector<long long> a(n + 2);
    for (long long i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    long long ans = 0;
    for (long long i = 1; i <= n; ++i) {
      if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
        ans += a[i];
      } else if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
        ans -= a[i];
      }
    }
    cout << ans << endl;
    for (long long q123 = 0; q123 < q; ++q123) {
      long long l, r;
      cin >> l >> r;
      ans += erase(l - 1, a) + erase(l, a) + erase(l + 1, a);
      if (l != r) {
        if (r - 1 != l + 1 && r - 1 != l) ans += erase(r - 1, a);
        if (r != l + 1) ans += erase(r, a);
        ans += erase(r + 1, a);
      }
      swap(a[l], a[r]);
      ans += insert(l - 1, a) + insert(l, a) + insert(l + 1, a);
      if (l != r) {
        if (r - 1 != l + 1 && r - 1 != l) ans += insert(r - 1, a);
        if (r != l + 1) ans += insert(r, a);
        ans += insert(r + 1, a);
      }
      cout << ans << endl;
    }
  }
}
