#include <bits/stdc++.h>
using namespace std;
long long int n, q;
vector<long long int> v, type;
void update(int i) {
  if (i == 0 || i == n - 1) return;
  if (v[i] > v[i - 1] && v[i] > v[i + 1])
    type[i] = 1;
  else if (v[i] < v[i - 1] && v[i] < v[i + 1])
    type[i] = -1;
  else
    type[i] = 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> q;
    n += 2;
    v.assign(n, 0);
    v[0] = -1e9, v[n - 1] = -1e9;
    for (int i = 1; i < n - 1; i++) cin >> v[i];
    type.assign(n, 0);
    for (int i = 1; i < n - 1; i++) update(i);
    long long int first_max = 0, last_max = 0, sum = 0;
    for (int i = 0; i < n; i++)
      if (type[i] == 1) {
        first_max = i;
        break;
      }
    for (int i = n - 1; i >= 0; i--)
      if (type[i] == 1) {
        last_max = i;
        break;
      }
    for (int i = first_max; i <= last_max; i++) {
      if (type[i] == 1) sum += v[i];
      if (type[i] == -1) sum -= v[i];
    }
    cout << sum << endl;
    for (int i = 0; i < (int)q; i++) {
      int l, r;
      cin >> l >> r;
      vector<int> w = {l - 1, l, l + 1, r - 1, r, r + 1};
      set<int> s;
      for (int i = 0; i < (int)w.size(); i++) s.insert(w[i]);
      w.clear();
      for (auto it : s) w.push_back(it);
      for (int i = 0; i < (int)w.size(); i++) {
        if (type[w[i]] == 1) sum -= v[w[i]];
        if (type[w[i]] == -1) sum += v[w[i]];
      };
      swap(v[l], v[r]);
      for (int i = 0; i < (int)w.size(); i++) update(w[i]);
      for (int i = 0; i < (int)w.size(); i++) {
        if (type[w[i]] == 1 && w[i] < first_max) first_max = w[i];
        if (type[w[i]] == 1 && w[i] > last_max) last_max = w[i];
      }
      for (int i = 0; i < (int)w.size(); i++) {
        if (w[i] >= first_max && w[i] <= last_max) {
          if (type[w[i]] == 1) sum += v[w[i]];
          if (type[w[i]] == -1) sum -= v[w[i]];
        }
      }
      cout << sum << endl;
    }
  }
  return 0;
}
