#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count());
long long n, q, sumL, sumH;
set<long long> low, high;
vector<long long> a;
void change(long long i, long long l, long long r) {
  if (i < 0 || i >= n) return;
  long long was = a[i];
  if (i == l) was = a[r];
  if (i == r) was = a[l];
  if (low.find(i) != low.end()) {
    low.erase(i);
    sumL -= was;
  }
  if ((i == 0 || a[i - 1] > a[i]) && (i == n - 1 || a[i] < a[i + 1])) {
    low.insert(i);
    sumL += a[i];
  }
  if (high.find(i) != high.end()) {
    high.erase(i);
    sumH -= was;
  }
  if ((i == 0 || a[i - 1] < a[i]) && (i == n - 1 || a[i] > a[i + 1])) {
    high.insert(i);
    sumH += a[i];
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> q;
    a.clear();
    a.resize(n);
    for (auto &i : a) cin >> i;
    if (n == 1) {
      cout << a[0] << '\n';
      while (q--) {
        long long l, r;
        cin >> l >> r;
        l--;
        r--;
        cout << a[0] << '\n';
      }
    } else {
      sumL = 0, sumH = 0;
      low.clear();
      high.clear();
      for (long long i = 0; i < n; i++)
        if ((i == 0 || a[i - 1] > a[i]) && (i == n - 1 || a[i] < a[i + 1]))
          low.insert(i);
      for (long long i = 0; i < n; i++)
        if ((i == 0 || a[i - 1] < a[i]) && (i == n - 1 || a[i] > a[i + 1]))
          high.insert(i);
      for (auto &i : low) sumL += a[i];
      for (auto &i : high) sumH += a[i];
      long long d = 0;
      if (*low.begin() < *high.begin()) d += a[*low.begin()];
      if (*low.rbegin() > *high.rbegin()) d += a[*low.rbegin()];
      cout << sumH - sumL + d << '\n';
      while (q--) {
        long long l, r;
        cin >> l >> r;
        l--;
        r--;
        swap(a[l], a[r]);
        set<long long> need;
        for (long long i = -1; i <= 1; i++) {
          need.insert(l + i);
          need.insert(r + i);
        }
        for (auto &i : need) change(i, l, r);
        d = 0;
        if (*low.begin() < *high.begin()) d += a[*low.begin()];
        if (*low.rbegin() > *high.rbegin()) d += a[*low.rbegin()];
        cout << sumH - sumL + d << '\n';
      }
    }
  }
  return 0;
}
