#include <bits/stdc++.h>
using namespace std;
const long long M = 1e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
vector<int> v;
int pk[3 * M];
long long ans = 0;
int n, q;
void ck(int id, int pre) {
  if (v[id] == 0) return;
  if (pk[id] == 1) ans -= pre;
  if (pk[id] == -1) ans += pre;
  if (v[id] < v[id + 1] && v[id] < v[id - 1]) {
    ans -= v[id];
    pk[id] = -1;
  } else if (v[id] > v[id + 1] && v[id] > v[id - 1]) {
    ans += v[id];
    pk[id] = 1;
  } else
    pk[id] = 0;
}
void solve() {
  ans = 0;
  cin >> n >> q;
  for (int i = 0; i < n + 2; i++) pk[i] = 0;
  v.clear();
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  v.push_back(0);
  for (int i = 1; i <= n; i++) {
    if (v[i] > v[i - 1] && v[i] > v[i + 1])
      pk[i] = 1;
    else if (v[i] < v[i - 1] && v[i] < v[i + 1])
      pk[i] = -1;
  }
  for (int i = 1; i <= n; i++) {
    ans += pk[i] * v[i];
  }
  cout << ans << endl;
  while (q--) {
    int l, r;
    cin >> l >> r;
    set<int> st;
    int pre1 = v[l], pre2 = v[r], pre3 = v[l + 1], pre4 = v[r - 1];
    swap(v[l], v[r]);
    ck(l, pre1);
    st.insert(l);
    if (st.find(l - 1) == st.end()) ck(l - 1, v[l - 1]);
    st.insert(l - 1);
    if (st.find(l + 1) == st.end()) ck(l + 1, pre3);
    st.insert(l + 1);
    if (st.find(r) == st.end()) ck(r, pre2);
    st.insert(r);
    if (st.find(r + 1) == st.end()) ck(r + 1, v[r + 1]);
    st.insert(r + 1);
    if (st.find(r - 1) == st.end()) ck(r - 1, pre4);
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T;
  cin >> T;
  for (long long tc = 1; tc <= T; tc++) {
    solve();
  }
  return 0;
}
