#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const ll inf = 1e9;
const int mod = 1000000007;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int _ = 0; _ < (t); _++) {
    int n, q;
    cin >> n >> q;
    vector<ll> aa(n + 1, 0);
    for (int i = 0; i < (n); i++) cin >> aa[i + 1];
    ll ans = 0;
    for (int i = 0; i < (n); i++) ans += max(0LL, aa[i + 1] - aa[i]);
    cout << ans << endl;
    for (int qi = 0; qi < (q); qi++) {
      int l, r;
      cin >> l >> r;
      if (r - l == 1) {
        ans -= max(0LL, aa[l + 1] - aa[l]) + max(0LL, aa[l] - aa[l - 1]);
        if (r + 1 <= n) ans -= max(0LL, aa[r + 1] - aa[r]);
        swap(aa[l], aa[r]);
        ans += max(0LL, aa[l + 1] - aa[l]) + max(0LL, aa[l] - aa[l - 1]);
        if (r + 1 <= n) ans += max(0LL, aa[r + 1] - aa[r]);
      } else if (l != r) {
        ans -= max(0LL, aa[l + 1] - aa[l]) + max(0LL, aa[l] - aa[l - 1]) +
               max(0LL, aa[r] - aa[r - 1]);
        if (r + 1 <= n) ans -= max(0LL, aa[r + 1] - aa[r]);
        swap(aa[l], aa[r]);
        ans += max(0LL, aa[l + 1] - aa[l]) + max(0LL, aa[l] - aa[l - 1]) +
               max(0LL, aa[r] - aa[r - 1]);
        if (r + 1 <= n) ans += max(0LL, aa[r + 1] - aa[r]);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
