#include <bits/stdc++.h>
using namespace std;
int cal(int a) {
  if (a < 0) a = 0;
  return a;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int ttt;
  cin >> ttt;
  while (ttt-- > 0) {
    int n, q;
    cin >> n >> q;
    int temp = 0;
    vector<int> a(1, 0);
    long long res = 0;
    while (n--) {
      int t;
      cin >> t;
      if (t > temp) {
        res += t - temp;
      }
      temp = t;
      a.push_back(t);
    }
    a.push_back(0);
    cout << res << endl;
    while (q--) {
      int l, r;
      cin >> l >> r;
      res -= cal(a[l] - a[l - 1]) + cal(a[l + 1] - a[l]);
      res -= cal(a[r] - a[r - 1]) + cal(a[r + 1] - a[r]);
      if (l == r - 1) res += cal(a[l + 1] - a[l]);
      swap(a[l], a[r]);
      res += cal(a[l] - a[l - 1]) + cal(a[l + 1] - a[l]);
      res += cal(a[r] - a[r - 1]) + cal(a[r + 1] - a[r]);
      if (l == r - 1) res -= cal(a[l + 1] - a[l]);
      cout << res << endl;
    }
  }
}
