#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = (ll)1e18;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail>
void p(Head head, Tail... tail) {
  std::cout << head << (sizeof...(tail) ? " " : "");
  p(tail...);
}
template <typename T>
inline void pv(std::vector<T>& v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T>
inline bool chmax(T& a, T b) {
  return a < b && (a = b, true);
}
template <typename T>
inline bool chmin(T& a, T b) {
  return a > b && (a = b, true);
}
template <typename T>
inline void uniq(std::vector<T>& v) {
  v.erase(std::unique(v.begin(), v.end()), v.end());
}
void solve() {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> a(N + 2, -INF);
  for (ll i = 1, i_length = (N); i <= i_length; ++i) cin >> a[i];
  ll ans = 0;
  for (ll i = 1, i_length = (N); i <= i_length; ++i) {
    if (a[i - 1] < a[i] && a[i] > a[i + 1])
      ans += a[i];
    else if (a[i - 1] > a[i] && a[i] < a[i + 1])
      ans -= a[i];
  }
  p(ans);
  while (Q--) {
    ll l, r;
    cin >> l >> r;
    if (r < l) swap(l, r);
    if (r - 2 < l + 2) {
      for (ll i = max(1LL, l - 2); i <= min(r + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans -= a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans += a[i];
      }
      swap(a[l], a[r]);
      for (ll i = max(1LL, l - 2); i <= min(r + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans += a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans -= a[i];
      }
    } else {
      for (ll i = max(1LL, l - 2); i <= min(l + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans -= a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans += a[i];
      }
      for (ll i = max(1LL, r - 2); i <= min(r + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans -= a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans += a[i];
      }
      swap(a[l], a[r]);
      for (ll i = max(1LL, l - 2); i <= min(l + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans += a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans -= a[i];
      }
      for (ll i = max(1LL, r - 2); i <= min(r + 2, N); i++) {
        if (a[i - 1] < a[i] && a[i] > a[i + 1])
          ans += a[i];
        else if (a[i - 1] > a[i] && a[i] < a[i + 1])
          ans -= a[i];
      }
    }
    p(ans);
  }
}
signed main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll Q;
  cin >> Q;
  while (Q--) solve();
  return 0;
}
