#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int arr[maxn];
int cf[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
      cin >> arr[i];
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      cf[i] = arr[i] - arr[i - 1];
      if (cf[i] > 0) ans += cf[i];
    }
    cout << ans << endl;
    for (int i = 0; i < q; ++i) {
      int l, r;
      cin >> l >> r;
      if (cf[l] > 0) ans -= cf[l];
      if (l + 1 <= n && cf[l + 1] > 0) ans -= cf[l + 1];
      if (cf[r] > 0) ans -= cf[r];
      if (r + 1 <= n && cf[r + 1] > 0) ans -= cf[r + 1];
      if (l + 1 == r && cf[r] > 0) ans += cf[r];
      swap(arr[l], arr[r]);
      cf[l] = arr[l] - arr[l - 1];
      if (cf[l] > 0) ans += cf[l];
      if (l + 1 <= n) cf[l + 1] = arr[l + 1] - arr[l];
      if (l + 1 <= n && cf[l + 1] > 0) ans += cf[l + 1];
      cf[r] = arr[r] - arr[r - 1];
      if (cf[r] > 0) ans += cf[r];
      if (r + 1 <= n) cf[r + 1] = arr[r + 1] - arr[r];
      if (r + 1 <= n && cf[r + 1] > 0) ans += cf[r + 1];
      if (l + 1 == r && cf[r] > 0) ans -= cf[r];
      cout << ans << endl;
    }
  }
  return 0;
}
