#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using db = double;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VS = vector<string>;
const int inf = 0x3f3f3f3f;
const int rinf = 0xc0c0c0c0;
const LL MOD = 1e9 + 7;
const LL LMAX = 0x7fffffffffffffff;
const LL LMIN = 0x8000000000000000;
template <typename T1, typename T2, typename T3>
struct Tup {
  T1 first;
  T2 second;
  T3 third;
  inline bool operator<(const Tup<T1, T2, T3> &b) {
    if (this->first != b.first) {
      if (this->second != b.second)
        return this->second < b.second;
      else
        return this->third < b.third;
    }
    return this->first < b.first;
  }
  friend ostream &operator<<(ostream &out, const Tup<T1, T2, T3> &tup) {
    out << tup.first << " " << tup.second << " " << tup.third;
    return out;
  }
  void disp() {
    cout << "(" << this->first << ", " << this->second << ", " << this->third
         << ")\n";
  }
};
template <typename T>
void print(vector<vector<T>> a) {
  for (int i = 0; i < a.size(); ++i) {
    for (int j = 0; j < a[i].size(); ++j)
      cout << a[i][j] << (j == ((int)a[i].size()) - 1 ? '\n' : ' ');
  }
}
template <typename T>
void print(T *a, int len) {
  for (int i = 0; i < len; ++i) cout << a[i] << " ";
  cout << endl;
}
template <typename T>
void print(T **a, int row, int col) {
  for (int i = 0; i < row; ++i) {
    for (int j = 0; j < col; ++j)
      cout << a[i][j] << (j == col - 1 ? '\n' : ' ');
  }
}
template <typename T1, typename T2>
void print(const vector<pair<T1, T2>> a) {
  for (auto &e : a) cout << "(" << e.first << ", " << e.second << ")\n";
}
template <typename... Args>
void _SHOW_ALL(Args... args) {}
template <typename T, typename... Args>
void _SHOW_ALL(T val, Args... args) {
  cout << val << ", ";
  _SHOW_ALL(args...);
}
template <typename T, typename... Args>
void to_brackets(T val, Args... args) {
  cout << "(";
  _SHOW_ALL(val, args...);
  cout << ")\n";
}
template <typename... Args>
void wprint(Args... args) {
  cout << '\n';
}
template <typename T, typename... Args>
void wprint(T val, Args... args) {
  cout << val << " ";
  wprint(args...);
}
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return f * x;
}
inline LL readl() {
  char c = getchar();
  LL x = 0, f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return f * x;
}
void write(int a) {
  if (a < 0) putchar('-'), a = -a;
  if (a >= 10) write(a / 10);
  putchar(a % 10 + 48);
}
const int maxn = 3e5 + 50;
LL f[maxn];
void solve() {
  int n = read(), q = read();
  for (int i = 1; i <= n; ++i) f[i] = readl();
  f[0] = f[n + 1] = -1;
  LL ans(0);
  for (int i = 1; i <= n; ++i) {
    if (f[i - 1] > f[i] && f[i] < f[i + 1]) ans -= f[i];
    if (f[i - 1] < f[i] && f[i] > f[i + 1]) ans += f[i];
  }
  cout << ans << '\n';
  auto &&add = [&](int i) {
    if (i == 0 or i == n + 1) return;
    if (f[i - 1] > f[i] and f[i] < f[i + 1]) ans -= f[i];
    if (f[i - 1] < f[i] and f[i] > f[i + 1]) ans += f[i];
  };
  auto &&del = [&](int i) {
    if (i == 0 or i == n + 1) return;
    if (f[i - 1] > f[i] and f[i] < f[i + 1]) ans += f[i];
    if (f[i - 1] < f[i] and f[i] > f[i + 1]) ans -= f[i];
  };
  for (int i = 0; i < q; ++i) {
    int l = read(), r = read();
    del(l - 1), del(l), del(l + 1);
    if (l != r) {
      if (r - 1 != l + 1 and r - 1 != l) del(r - 1);
      if (r != l + 1) del(r);
      del(r + 1);
    }
    swap(f[l], f[r]);
    add(l - 1), add(l), add(l + 1);
    if (l != r) {
      if (r - 1 != l + 1 and r - 1 != l) add(r - 1);
      if (r != l + 1) add(r);
      add(r + 1);
    }
    cout << ans << '\n';
  }
}
int main() {
  int t = read();
  while (t--) solve();
  return 0;
}
