#include <bits/stdc++.h>
long long q_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = x * ret % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
long long __gcd(long long x, long long y) {
  if (!y) return x;
  return __gcd(y, x % y);
}
long long getbit(long long now) {
  int cnt = 0;
  while (now) {
    cnt++;
    now -= (now) & (-now);
  }
  return cnt;
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long IINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 3e5 + 9;
const int maxm = 2520 + 5;
const double eps = 1e-7;
const long long mod = 998244353;
int a[maxn];
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  a[n + 1] = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] - a[i + 1] >= 0) ans += a[i] - a[i + 1];
  printf("%lld\n", ans);
  for (int i = 1; i <= m; i++) {
    int le, ri;
    scanf("%d%d", &le, &ri);
    ans -= max(a[le - 1] - a[le], 0);
    ans -= max(a[le] - a[le + 1], 0);
    if (le + 1 != ri) ans -= max(a[ri - 1] - a[ri], 0);
    ans -= max(a[ri] - a[ri + 1], 0);
    swap(a[le], a[ri]);
    ans += max(a[le - 1] - a[le], 0);
    ans += max(a[le] - a[le + 1], 0);
    if (le + 1 != ri) ans += max(a[ri - 1] - a[ri], 0);
    ans += max(a[ri] - a[ri + 1], 0);
    printf("%lld\n", ans);
  }
}
signed main(int argc, char *argv[]) {
  if (argc == 2 && strcmp("-debug", argv[1]) == 0) {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
