#include <bits/stdc++.h>
using namespace std;
using namespace std;
using ld = long double;
const long long MOD = 1e9 + 7, inf = 1e9, N = 2e5 + 1;
long long hp(long long n) {
  long long p = (long long)log2(n);
  long long r = 1;
  for (long long i = 1; i <= p; i++) r *= 2;
  return r;
}
void solve() {
  long long n, q;
  cin >> n >> q;
  long long a[n];
  for (long long i = 0; i < (n); ++i) cin >> a[i];
  long long li, ri;
  if (n == 1) {
    cout << a[0] << '\n';
    for (long long i = 0; i < (q); ++i) {
      cin >> li >> ri;
      cout << a[0] << '\n';
    }
    return;
  }
  set<long long> maxi, mini;
  for (long long i = 0; i < n; i++) {
    if (i == 0) {
      if (a[i] > a[i + 1]) maxi.insert(i);
      continue;
    } else if (i == n - 1) {
      if (a[i] > a[i - 1]) maxi.insert(i);
    } else if (a[i] > a[i - 1] && a[i] > a[i + 1])
      maxi.insert(i);
    else if (a[i] < a[i - 1] && a[i] < a[i + 1])
      mini.insert(i);
  }
  long long ans = 0;
  for (auto &it : maxi) ans += a[it];
  for (auto &it : mini) ans -= a[it];
  cout << ans << '\n';
  for (long long i = 0; i < (q); ++i) {
    cin >> li >> ri;
    li--;
    ri--;
    swap(a[li], a[ri]);
    set<long long> v1;
    v1.insert(li);
    v1.insert(ri);
    if (li + 1 < n) v1.insert(li + 1);
    if (li - 1 >= 0) v1.insert(li - 1);
    if (ri + 1 < n) v1.insert(ri + 1);
    if (ri - 1 >= 0) v1.insert(ri - 1);
    for (auto &i : v1) {
      if (i == li || i == ri) {
        if (maxi.find(i) != maxi.end()) {
          maxi.erase(i);
          ans -= a[li + ri - i];
        }
        if (mini.find(i) != mini.end()) {
          mini.erase(i);
          ans += a[li + ri - i];
        }
      } else {
        if (maxi.find(i) != maxi.end()) {
          maxi.erase(i);
          ans -= a[i];
        }
        if (mini.find(i) != mini.end()) {
          mini.erase(i);
          ans += a[i];
        }
      }
    }
    for (auto &i : v1) {
      if (i == 0) {
        if (a[i] > a[i + 1]) {
          maxi.insert(i);
          ans += a[i];
        }
      } else if (i == n - 1) {
        if (a[i] > a[i - 1]) {
          maxi.insert(i);
          ans += a[i];
        }
      } else if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
        maxi.insert(i);
        ans += a[i];
      } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
        mini.insert(i);
        ans -= a[i];
      }
    }
    cout << ans << '\n';
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
