#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
long long n, x, m, tmp, ans, t, k, q;
long long a[N], b[N], h[N];
vector<long long> v[N];
bool mark;
set<int> s;
void read_input() {
  for (int i = 0; i < N; i++) {
    a[i] = 0;
    v[i].clear();
  }
  ans = 0;
  mark = false;
  tmp = 0;
  cin >> n >> k;
  b[0] = -1;
  b[n + 1] = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i + 1] = a[i];
  }
  v[tmp].push_back(a[0]);
  for (int i = 1; i < n; i++) {
    if (!mark) {
      if (a[i] >= a[i - 1]) {
        v[tmp].push_back(a[i]);
      } else {
        mark = !mark;
        tmp++;
        v[tmp].push_back(a[i]);
      }
    } else {
      if (a[i] < a[i - 1]) {
        v[tmp].push_back(a[i]);
      } else {
        mark = !mark;
        tmp++;
        v[tmp].push_back(a[i]);
      }
    }
  }
  if (tmp % 2 == 1) {
    tmp--;
  }
}
void solve() {
  while (k--) {
    s.clear();
    int l, r;
    cin >> l >> r;
    if (l == r) {
      cout << ans << '\n';
      continue;
    }
    s.insert(l - 1), s.insert(l), s.insert(l + 1);
    s.insert(r - 1), s.insert(r), s.insert(r + 1);
    for (auto it = s.begin(); it != s.end(); it++) {
      int i = *it;
      if (i < 1 || i > n) {
        continue;
      }
      if (b[i] > b[i - 1] && b[i] > b[i + 1]) {
        ans -= b[i];
      }
      if (b[i] < b[i - 1] && b[i] < b[i + 1]) {
        ans += b[i];
      }
    }
    swap(b[l], b[r]);
    for (auto it = s.begin(); it != s.end(); it++) {
      int i = *it;
      if (i < 1 || i > n) {
        continue;
      }
      if (b[i] > b[i - 1] && b[i] > b[i + 1]) {
        ans += b[i];
      }
      if (b[i] < b[i - 1] && b[i] < b[i + 1]) {
        ans -= b[i];
      }
    }
    cout << ans << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    read_input();
    for (int i = 0; i <= tmp; i++) {
      if (i % 2 == 0) {
        ans += v[i].back();
      } else {
        ans -= v[i].back();
      }
    }
    cout << ans << '\n';
    solve();
  }
  return 0;
}
