#include <bits/stdc++.h>
using namespace std;
long long int ans, n;
long long int arr[300005];
void insert(long long int i) {
  if (i < 1 || i > n) return;
  if (arr[i] > arr[i + 1] && arr[i] > arr[i - 1]) ans += arr[i];
  if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1]) ans -= arr[i];
}
void erase(long long int i) {
  if (i < 1 || i > n) return;
  if (arr[i] > arr[i + 1] && arr[i] > arr[i - 1]) ans -= arr[i];
  if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1]) ans += arr[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int q;
    cin >> n >> q;
    ans = 0;
    for (long long int i = 1; i <= n; i++) cin >> arr[i];
    arr[0] = -1;
    arr[n + 1] = -1;
    for (long long int i = 1; i <= n; i++) {
      if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) ans += arr[i];
      if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) ans -= arr[i];
    }
    cout << ans << endl;
    while (q--) {
      long long int l, r;
      cin >> l >> r;
      if (l == r) {
        cout << ans << endl;
        continue;
      }
      erase(l);
      erase(l - 1);
      erase(l + 1);
      if (r > l + 1) erase(r);
      if (r - 1 > l + 1) erase(r - 1);
      erase(r + 1);
      swap(arr[l], arr[r]);
      insert(l);
      insert(l - 1);
      insert(l + 1);
      if (r > l + 1) insert(r);
      if (r - 1 > l + 1) insert(r - 1);
      insert(r + 1);
      cout << ans << endl;
    }
  }
  return 0;
}
