#include <bits/stdc++.h>
using namespace std;
template <class T>
using VV = vector<vector<T>>;
template <class T>
inline T SMIN(T& a, const T b) {
  return a = (a > b) ? b : a;
}
template <class T>
inline T SMAX(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class Iter>
void __kumaerrc(Iter begin, Iter end) {
  for (; begin != end; ++begin) {
    cerr << *begin << ',';
  }
  cerr << endl;
}
void __kumaerr(istream_iterator<string> it) {
  (void)it;
  cerr << endl;
}
template <typename T, typename... Args>
void __kumaerr(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << "=" << a << ", ", __kumaerr(++it, args...);
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& _os, const std::pair<S, T>& _p) {
  return _os << "{" << _p.first << ',' << _p.second << "}";
}
const int MAX_N = 1e6 + 1;
long long A[MAX_N];
int N, Q;
void solve() {
  ++N;
  long long res = 0;
  auto reset = [&](int i) {
    int a = A[i];
    long long x = max(0LL, a - A[i - 1]);
    res -= x;
  };
  auto upd = [&](int i) { res += max(0LL, A[i] - A[i - 1]); };
  for (int i = (1); i < (N); ++i) upd(i);
  cout << res << endl;
  for (int _ = 0; _ < (Q); ++_) {
    int l, r;
    cin >> l >> r;
    if (r != l) {
      if (r > l + 1) {
        reset(l);
        if (l < N - 1) reset(l + 1);
        reset(r);
        if (r < N - 1) reset(r + 1);
      } else {
        reset(l), reset(l + 1);
        if (l + 2 < N) reset(l + 2);
      }
      swap(A[l], A[r]);
      if (r > l + 1) {
        upd(l);
        if (l < N - 1) upd(l + 1);
        upd(r);
        if (r < N - 1) upd(r + 1);
      } else {
        upd(l), upd(l + 1);
        if (l + 2 < N) upd(l + 2);
      }
    }
    cout << res << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(12) << fixed;
  int t;
  cin >> t;
  while (t--) {
    cin >> N >> Q;
    A[0] = 0;
    for (int i = 0; i < (N); ++i) cin >> A[i + 1];
    solve();
    for (int i = 0; i < (N); ++i) A[i] = 0;
  }
  return 0;
}
