#include <bits/stdc++.h>
using std::max;
using std::min;
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3f;
__inline__ __attribute__((always_inline)) unsigned int rnd() {
  static unsigned int seed = 416;
  return seed += 0x71dad4bfu, seed ^= seed >> 5, seed += 0xc6f74d88u,
         seed ^= seed << 17, seed += 0x25e6561u, seed ^= seed >> 13;
}
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch)) f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  f && (x = -x);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b,
                                                  const Args &...args) {
  return a < b ? min(a, args...) : min(b, args...);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b,
                                                  const Args &...args) {
  return a < b ? max(b, args...) : max(a, args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
  char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
  char *tar = s;
  *tar = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *(++tar) = ch, ch = getchar();
  return tar - s + 1;
}
const int N = 300005;
int a[N];
int main() {
  int _;
  read(_);
  while (_--) {
    int n, q;
    read(n, q);
    a[n + 1] = 0;
    for (int i = 1; i <= n; ++i) read(a[i]);
    long long ans = 0;
    for (int i = 1; i <= n; ++i) ans += max(a[i] - a[i - 1], 0);
    printf("%lld\n", ans);
    while (q--) {
      int x, y;
      read(x, y);
      ans -= max(a[x] - a[x - 1], 0);
      ans -= max(a[x + 1] - a[x], 0);
      if (y != x && y != x + 1) ans -= max(a[y] - a[y - 1], 0);
      if (y + 1 != x && y + 1 != x + 1) ans -= max(a[y + 1] - a[y], 0);
      std::swap(a[x], a[y]);
      ans += max(a[x] - a[x - 1], 0);
      ans += max(a[x + 1] - a[x], 0);
      if (y != x && y != x + 1) ans += max(a[y] - a[y - 1], 0);
      if (y + 1 != x && y + 1 != x + 1) ans += max(a[y + 1] - a[y], 0);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
