#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char ch) { return string("'") + ch + string("'"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class InputIterator>
string to_string(InputIterator first, InputIterator last) {
  bool start = true;
  string res = "{";
  while (first != last) {
    if (!start) {
      res += ", ";
    }
    start = false;
    res += to_string(*first);
    ++first;
  }
  res += "}";
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, size_t N>
string to_string(A (&v)[N]) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < N; i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(const bitset<N>& v) {
  string res = "";
  for (int i = 0; i < (int)v.size(); ++i) {
    res += char('0' + v[i]);
  }
  return res;
}
string to_string(const vector<bool>& v) {
  string res = "";
  for (int i = 0; i < (int)v.size(); ++i) {
    res += char('0' + v[i]);
  }
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head&& H, Tail&&... T) {
  cerr << " " << to_string(forward<Head>(H));
  debug_out(forward<Tail>(T)...);
}
const double PI = acos(-1);
const long long mod = 1e9 + 7;
template <class T>
bool smin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool smax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <typename T1, typename T2>
T1 powz(T1 A, T2 B);
const int MXN = 1e6 + 5;
const long long INF = 9e18 + 200;
const int inf = 1e9 + 100;
const int mxn = 2e5 + 20;
void test_case() {
  int n, q;
  cin >> n >> q;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  auto Get = [&](int i) {
    if (i < 0 || i >= n) return 0;
    int cur = a[i];
    int nxt = (i == n - 1 ? 0 : a[i + 1]);
    return max(cur - nxt, 0);
  };
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += Get(i);
  }
  cout << ans << '\n';
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    if (x < y) {
      ans -= Get(x - 1);
      ans -= Get(x);
      if (x != y - 1) {
        ans -= Get(y - 1);
      }
      ans -= Get(y);
      swap(a[x], a[y]);
      ans += Get(x - 1);
      ans += Get(x);
      if (x != y - 1) {
        ans += Get(y - 1);
      }
      ans += Get(y);
    }
    cout << ans << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int Tests = 1;
  cin >> Tests;
  for (int tc = 1; tc <= Tests; ++tc) {
    test_case();
  }
}
template <typename T1, typename T2>
T1 powz(T1 X, T2 B) {
  long long A = X;
  long long res = 1;
  A %= mod;
  assert(B >= 0);
  for (; B; B >>= 1) {
    if (B & 1) res = res * A % mod;
    A = A * A % mod;
  }
  return res;
}
