#include <bits/stdc++.h>
using namespace std;
const long long int hell = (long long int)(1e18 + 100LL);
const double pi = acos(-1.0);
long long int ans = 0;
void add(long long int i, vector<long long int>& ar, long long int n) {
  if (i <= 0 || i > n) return;
  if (ar[i] > ar[i + 1] && ar[i] > ar[i - 1]) ans += ar[i];
  if (ar[i] < ar[i + 1] && ar[i] < ar[i - 1]) ans -= ar[i];
}
void remove(long long int i, vector<long long int>& ar, long long int n) {
  if (i <= 0 || i > n) return;
  if (ar[i] > ar[i + 1] && ar[i] > ar[i - 1]) ans -= ar[i];
  if (ar[i] < ar[i + 1] && ar[i] < ar[i - 1]) ans += ar[i];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t, i, j, k, m, n, x, y, z;
  t = 1;
  cin >> t;
  while (t--) {
    long long int flag = 0, K;
    cin >> n >> m;
    vector<long long int> ar(n + 5, 0);
    for (i = 1; i <= n; i++) {
      cin >> ar[i];
    }
    ar[0] = ar[n + 1] = -1;
    ans = 0;
    for (i = 1; i <= n; i++) {
      if (ar[i] > ar[i - 1] && ar[i] > ar[i + 1]) {
        ans += ar[i];
      }
      if (ar[i] < ar[i - 1] && ar[i] < ar[i + 1]) {
        ans -= ar[i];
      }
    }
    cout << ans << "\n";
    while (m--) {
      long long int l, r;
      cin >> l >> r;
      set<long long int> S;
      S.insert(l);
      S.insert(l - 1);
      S.insert(l + 1);
      S.insert(r);
      S.insert(r - 1);
      S.insert(r + 1);
      for (auto it : S) {
        remove(it, ar, n);
      }
      swap(ar[l], ar[r]);
      for (auto it : S) {
        add(it, ar, n);
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
