#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long mul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long binexp(long long a, long long b) {
  if (b == 0) return 1;
  long long half = binexp(a, b / 2);
  if ((b % 2) == 0)
    return mul(half, half);
  else
    return mul(a, mul(half, half));
}
int change(long long arr[], int index, long long newval, int n) {
  int temp1 = 0;
  if (index > 0)
    temp1 += max(arr[index] - arr[index - 1], 0LL);
  else
    temp1 = arr[index];
  if (index + 1 < n) temp1 += max(0LL, arr[index + 1] - arr[index]);
  arr[index] = newval;
  int temp2 = 0;
  if (index > 0)
    temp2 += max(arr[index] - arr[index - 1], 0LL);
  else
    temp2 = arr[index];
  if (index + 1 < n) temp2 += max(0LL, arr[index + 1] - arr[index]);
  return (temp2 - temp1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n, q;
    cin >> n >> q;
    long long arr[n];
    for (long long i = 0; i < n; ++i) {
      cin >> arr[i];
    }
    long long sum = arr[0];
    set<int> indexes;
    for (long long i = 1; i < n; ++i) {
      sum += max(0LL, arr[i] - arr[i - 1]);
    }
    cout << sum << endl;
    while (q--) {
      long long l, r;
      cin >> l >> r;
      l -= 1;
      r -= 1;
      int temp = arr[l];
      int temp2 = arr[r];
      sum += change(arr, l, temp2, n);
      sum += change(arr, r, temp, n);
      cout << sum << endl;
    }
  }
  return 0;
}
