#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, q;
    cin >> n >> q;
    long long a[n + 2], ans = 0;
    a[0] = 0;
    for (int i = 1; i <= n; i++) cin >> a[i];
    a[n + 1] = 0;
    vector<int> v(n + 2, 0);
    for (int i = 1; i <= n; i++) {
      if (a[i - 1] > a[i] && a[i + 1] >= a[i]) v[i] = -1;
      if (a[i - 1] < a[i] && a[i + 1] <= a[i]) v[i] = 1;
      ans += v[i] * a[i];
    }
    cout << ans << endl;
    while (q--) {
      int x, y;
      cin >> x >> y;
      unordered_set<int> affected;
      for (int i = -1; i < 2; i++) {
        if (x + i > 0 && x + i <= n) affected.insert(x + i);
        if (y + i > 0 && y + i <= n) affected.insert(y + i);
      }
      for (auto i : affected) {
        ans -= a[i] * v[i];
      }
      swap(a[x], a[y]);
      for (auto i : affected) {
        if (a[i - 1] > a[i] && a[i + 1] >= a[i])
          v[i] = -1;
        else if (a[i - 1] < a[i] && a[i + 1] <= a[i])
          v[i] = 1;
        else
          v[i] = 0;
        ans += a[i] * v[i];
      }
      cout << ans << endl;
    }
  }
  return 0;
}
