#include <bits/stdc++.h>
using namespace std;
int T, n, q, a[300010];
struct node {
  int l, r, mid;
  long long f[2][2];
  node *ch[2];
  void check() {
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        f[i][j] = max(ch[0]->f[i][j], ch[1]->f[i][j]);
        for (int k = 0; k < 2; k++) {
          f[i][j] = max(f[i][j], ch[0]->f[i][k] + ch[1]->f[k ^ 1][j]);
        }
      }
    }
  }
  node(int x, int y) : l(x), r(y), mid(l + r >> 1) {
    if (x < y) {
      ch[0] = new node(l, mid);
      ch[1] = new node(mid + 1, r);
      check();
    } else {
      ch[0] = ch[1] = NULL;
      f[0][0] = a[l], f[1][1] = -a[l];
      f[0][1] = f[1][0] = 0;
    }
  }
  void change(int x) {
    if (l == r) return (void)(f[0][0] = a[l], f[1][1] = -a[l]);
    ch[x >= mid + 1]->change(x);
    check();
  }
} *root = NULL;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    root = new node(1, n);
    printf("%lld\n", root->f[0][0]);
    for (int i = 1, x, y; i <= q; i++) {
      scanf("%d %d", &x, &y);
      swap(a[x], a[y]);
      root->change(x);
      root->change(y);
      printf("%lld\n", root->f[0][0]);
    }
  }
}
