#include <bits/stdc++.h>
using namespace std;
const long long inf = INT_MAX;
const long long mod = 1e9 + 7;
const long long N = 3e5 + 5;
long long n, q, ans;
long long a[N];
void ADD(long long idx) {
  if (a[idx] > a[idx + 1] and a[idx] > a[idx - 1]) {
    ans += a[idx];
  }
  if (a[idx] < a[idx + 1] and a[idx] < a[idx - 1]) {
    ans -= a[idx];
  }
}
void REMOVE(long long idx) {
  if (a[idx] > a[idx + 1] and a[idx] > a[idx - 1]) {
    ans -= a[idx];
  }
  if (a[idx] < a[idx + 1] and a[idx] < a[idx - 1]) {
    ans += a[idx];
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> q;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    a[0] = a[n + 1] = ans = 0;
    for (long long i = 1; i <= n; i++) ADD(i);
    cout << ans << " \n ";
    while (q--) {
      long long l, r;
      cin >> l >> r;
      set<long long> cnt;
      for (long long j = -1; j <= 1; j++) {
        long long e1 = l + j;
        long long e2 = r + j;
        if (0 < e1 && e1 < n + 1) cnt.insert(e1);
        if (0 < e2 && e2 < n + 1) cnt.insert(e2);
      }
      for (long long x : cnt) REMOVE(x);
      swap(a[l], a[r]);
      for (long long x : cnt) ADD(x);
      cout << ans << " \n ";
    }
  }
  return 0;
}
