#include <bits/stdc++.h>
using namespace std;
template <typename T>
void readin(T &x) {
  x = 0;
  T fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fh;
}
void d_read(double &x) {
  x = 0.0;
  int fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
  if (ch == '.') {
    double num = 1.0;
    ch = getchar();
    for (; isdigit(ch); ch = getchar()) x = x + (num /= 10) * (ch ^ 48);
  }
  x *= fh;
}
template <typename T>
void wt(T x) {
  if (x > 9) wt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x, char c) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  wt(x);
  putchar(c);
}
int a[300005], d[300005];
long long t[1200005];
void build(int p, int l, int r) {
  if (l == r)
    t[p] = (d[l] >= 0) ? 0 : -d[l];
  else {
    int mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    t[p] = t[p << 1] + t[p << 1 | 1];
  }
}
void modify(int p, int l, int r, int pos) {
  if (l == pos && pos == r) {
    t[p] = (d[l] >= 0) ? 0 : -d[l];
  } else {
    int mid = l + r >> 1;
    if (pos <= mid)
      modify(p << 1, l, mid, pos);
    else
      modify(p << 1 | 1, mid + 1, r, pos);
    t[p] = t[p << 1] + t[p << 1 | 1];
  }
}
int main() {
  int T;
  readin(T);
  while (T--) {
    int n, q;
    readin(n);
    readin(q);
    a[n + 1] = 0;
    for (int i = 1; i <= n; i++) readin(a[i]);
    for (int i = 1; i <= n + 1; i++) d[i] = a[i] - a[i - 1];
    build(1, 1, n + 1);
    writeln(t[1], '\n');
    int li, ri;
    for (int i = 1; i <= q; i++) {
      readin(li);
      readin(ri);
      swap(a[li], a[ri]);
      d[li] = a[li] - a[li - 1];
      d[li + 1] = a[li + 1] - a[li];
      d[ri] = a[ri] - a[ri - 1];
      d[ri + 1] = a[ri + 1] - a[ri];
      modify(1, 1, n + 1, li);
      modify(1, 1, n + 1, li + 1);
      modify(1, 1, n + 1, ri);
      modify(1, 1, n + 1, ri + 1);
      writeln(t[1], '\n');
    }
  }
  return 0;
}
