#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T>
ostream &operator<<(ostream &os, vector<T> &v);
template <class T>
ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2>
ostream &operator<<(ostream &os, map<T1, T2> &v);
const int N = 2e5 + 5;
const long long mod = 998244353;
void check1(long long &ans, long long l, vector<long long> &a) {
  if (a[l] > a[l - 1] && a[l] > a[l + 1]) {
    ans -= a[l];
  }
  if (a[l] < a[l - 1] && a[l] < a[l + 1]) {
    ans += a[l];
  }
}
void check2(long long &ans, long long l, vector<long long> &a) {
  if (a[l] > a[l - 1] && a[l] > a[l + 1]) {
    ans += a[l];
  }
  if (a[l] < a[l - 1] && a[l] < a[l + 1]) {
    ans -= a[l];
  }
}
void TEST_CASES(int cas) {
  long long n, q;
  cin >> n >> q;
  vector<long long> a(n + 2);
  a[0] = 0;
  vector<long long> d(n);
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[n + 1] = 0;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
      ans += a[i];
    }
    if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
      ans -= a[i];
    }
  }
  cout << ans << "\n";
  while (q--) {
    long long l, r;
    cin >> l >> r;
    map<long long, bool> vis;
    for (long long i = l - 1; i <= l + 1; i++) {
      if (i >= 1 && i <= n && !vis[i]) {
        vis[i] = true;
        check1(ans, i, a);
      }
    }
    for (long long i = r - 1; i <= r + 1; i++) {
      if (i >= 1 && i <= n && !vis[i]) {
        vis[i] = true;
        check1(ans, i, a);
      }
    };
    swap(a[l], a[r]);
    vis.clear();
    for (long long i = l - 1; i <= l + 1; i++) {
      if (i >= 1 && i <= n && !vis[i]) {
        vis[i] = true;
        check2(ans, i, a);
      }
    }
    for (long long i = r - 1; i <= r + 1; i++) {
      if (i >= 1 && i <= n && !vis[i]) {
        vis[i] = true;
        check2(ans, i, a);
      }
    };
    cout << ans << "\n";
    ;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1, cas = 0;
  cin >> t;
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}
