#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const long long INF = 1e18 + 5;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, q;
    cin >> n >> q;
    vector<int> A(n + 2);
    for (int in = 1; in < 1 + n; in++) cin >> A[in];
    A[0] = -n - 1;
    A[n + 1] = -n - 1;
    long long ans = 0;
    set<int> Pos;
    set<int> Neg;
    for (int i = 1; i <= n; i++) {
      if (A[i] > A[i - 1] && A[i] > A[i + 1]) {
        ans += A[i];
        Pos.insert(A[i]);
      }
      if (A[i] < A[i - 1] && A[i] < A[i + 1]) {
        ans -= A[i];
        Neg.insert(A[i]);
      }
    }
    cout << ans << '\n';
    while (q--) {
      int u, v;
      cin >> u >> v;
      swap(A[u], A[v]);
      long long diff = 0;
      for (int i = u - 1; i <= u + 1; i++) {
        if (i <= 0) continue;
        if (i > n) continue;
        if (A[i] > A[i - 1] && A[i] > A[i + 1]) {
          if (Neg.count(A[i])) {
            Neg.erase(A[i]);
            Pos.insert(A[i]);
            diff += 2 * A[i];
          } else if (!Pos.count(A[i])) {
            Pos.insert(A[i]);
            diff += A[i];
          }
        } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) {
          if (Pos.count(A[i])) {
            Pos.erase(A[i]);
            Neg.insert(A[i]);
            diff -= 2 * A[i];
          } else if (!Neg.count(A[i])) {
            Neg.insert(A[i]);
            diff -= A[i];
          }
        } else {
          if (Pos.count(A[i])) {
            Pos.erase(A[i]);
            diff -= A[i];
          } else if (Neg.count(A[i])) {
            Neg.erase(A[i]);
            diff += A[i];
          }
        }
      }
      for (int i = v - 1; i <= v + 1; i++) {
        if (i <= u + 1) continue;
        if (i <= 0) continue;
        if (i > n) continue;
        if (A[i] > A[i - 1] && A[i] > A[i + 1]) {
          if (Neg.count(A[i])) {
            Neg.erase(A[i]);
            Pos.insert(A[i]);
            diff += 2 * A[i];
          } else if (!Pos.count(A[i])) {
            Pos.insert(A[i]);
            diff += A[i];
          }
        } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) {
          if (Pos.count(A[i])) {
            Pos.erase(A[i]);
            Neg.insert(A[i]);
            diff -= 2 * A[i];
          } else if (!Neg.count(A[i])) {
            Neg.insert(A[i]);
            diff -= A[i];
          }
        } else {
          if (Pos.count(A[i])) {
            Pos.erase(A[i]);
            diff -= A[i];
          } else if (Neg.count(A[i])) {
            Neg.erase(A[i]);
            diff += A[i];
          }
        }
      }
      ans += diff;
      cout << ans << '\n';
    }
  }
}
