#include <bits/stdc++.h>
using namespace std;
int t, n, q, l, r;
long long ans, arr[300010];
void printArray(int *, int);
void ADD(int idx) {
  if (arr[idx] > arr[idx - 1] & arr[idx] > arr[idx + 1]) ans += arr[idx];
  if (arr[idx] < arr[idx - 1] & arr[idx] < arr[idx + 1]) ans -= arr[idx];
}
void REMOVE(int idx) {
  if (arr[idx] > arr[idx - 1] & arr[idx] > arr[idx + 1]) ans -= arr[idx];
  if (arr[idx] < arr[idx - 1] & arr[idx] < arr[idx + 1]) ans += arr[idx];
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
    arr[0] = arr[n + 1] = ans = 0;
    for (int i = 1; i <= n; i++) ADD(i);
    printf("%lld\n", ans);
    for (int i = 0; i < q; i++) {
      scanf("%d %d", &l, &r);
      set<int> tobeChanged;
      for (int i = -1; i <= 1; i++) {
        if (0 < (l + i) && (l + i) <= n) tobeChanged.insert(l + i);
        if (0 < (r + i) && (r + i) <= n) tobeChanged.insert(r + i);
      }
      for (int idx : tobeChanged) REMOVE(idx);
      swap(arr[l], arr[r]);
      for (int idx : tobeChanged) ADD(idx);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
void printArray(int *nums, int n) {
  for (int i = 0; i < n; i++) printf("%d ", nums[i]);
  printf("\n");
}
