#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
long long int mod(long long int x) { return (x % MOD + MOD) % MOD; }
long long int a[300014];
long long int ans;
void add(long long int i) {
  if (a[i] > a[i - 1] and a[i] > a[i + 1]) ans += a[i];
  if (a[i] < a[i - 1] and a[i] < a[i + 1]) ans -= a[i];
}
void remove(long long int i) {
  if (a[i] > a[i - 1] and a[i] > a[i + 1]) ans -= a[i];
  if (a[i] < a[i - 1] and a[i] < a[i + 1]) ans += a[i];
}
void solve() {
  long long int n, q;
  cin >> n >> q;
  for (long long int i = 1; i < n + 1; i++) cin >> a[i];
  a[0] = 0;
  a[n + 1] = 0;
  ans = 0;
  for (long long int i = 1; i < n + 1; i++) add(a[i]);
  cout << ans << endl;
  for (long long int i = 0; i < q; i++) {
    long long int x, y;
    cin >> x >> y;
    set<long long int> tochange;
    for (long long int i = -1; i <= 1; i++) {
      if (0 < (x + i) and (x + i) <= n) tochange.insert(x + i);
      if (0 < (y + i) and (y + i) <= n) tochange.insert(y + i);
    }
    for (auto ind : tochange) {
      remove(ind);
    }
    swap(a[x], a[y]);
    for (auto ind : tochange) {
      add(ind);
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
