#include <bits/stdc++.h>
using namespace std;
struct node {
  long long min0, max0, min1, max1;
  node() {
    min0 = max0 = min1 = max1 = 0;
    return;
  }
} tree[4000005];
long long t, n, q, a[300005];
inline void pushup(long long now) {
  tree[now].min0 = min(tree[now * 2].min0 + tree[now * 2 + 1].min0,
                       tree[now * 2].min1 - tree[now * 2 + 1].max1);
  tree[now].min1 = min(tree[now * 2].min0 + tree[now * 2 + 1].min1,
                       tree[now * 2].min1 - tree[now * 2 + 1].max0);
  tree[now].max0 = max(tree[now * 2].max0 + tree[now * 2 + 1].max0,
                       tree[now * 2].max1 - tree[now * 2 + 1].min1);
  tree[now].max1 = max(tree[now * 2].max0 + tree[now * 2 + 1].max1,
                       tree[now * 2].max1 - tree[now * 2 + 1].min0);
  return;
}
inline void build(long long now, long long nowl, long long nowr) {
  if (nowl == nowr) {
    tree[now].min0 = tree[now].max0 = 0;
    tree[now].min1 = tree[now].max1 = a[nowl];
    return;
  }
  long long mid = (nowl + nowr) / 2;
  build(now * 2, nowl, mid);
  build(now * 2 + 1, mid + 1, nowr);
  pushup(now);
  return;
}
inline void update(long long now, long long nowl, long long nowr,
                   long long pos) {
  if (nowl == nowr) {
    tree[now].min0 = tree[now].max0 = 0;
    tree[now].min1 = tree[now].max1 = a[nowl];
    return;
  }
  long long mid = (nowl + nowr) / 2;
  if (pos <= mid)
    update(now * 2, nowl, mid, pos);
  else
    update(now * 2 + 1, mid + 1, nowr, pos);
  pushup(now);
  return;
}
signed main() {
  cin >> t;
  while (t--) {
    cin >> n >> q;
    for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
    build(1, 1, n);
    printf("%lld\n", tree[1].max1);
    while (q--) {
      long long x, y;
      scanf("%lld%lld", &x, &y);
      swap(a[x], a[y]);
      update(1, 1, n, x);
      update(1, 1, n, y);
      printf("%lld\n", tree[1].max1);
    }
  }
  return 0;
}
