#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

const int N = (1 << 20);
struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init() {
    even_max.assign(N, 0);
    even_min.assign(N, 0);
    odd_max.assign(N, 0);
    odd_min.assign(N, 0);
  }

  void update(int v, int val, int l, int r, int L = 1, int R = N / 2) {
    if (l == L && r == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (l <= M) {
      update(2 * v, val, l, min(r, M), L, M);
    }
    if (r >= M + 1) {
      update(2 * v + 1, val, max(l, M + 1), r, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

void test_case() {
  SegmentTree tree;
  tree.init();

  int n, q;
  cin >> n >> q;
  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i, i);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j, j);
    tree.update(1, A[j], i, i);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

const int P = 998244353;

long long fact[300007];
long long inv_fact[300007];

long long bin_pow(long long n, long long k) {
  if (k == 0) {
    return 1;
  }
  if (k & 1) {
    return (n * bin_pow(n, k - 1)) % P;
  }
  long long tmp = bin_pow(n, k / 2);
  return (tmp * tmp) % P;
}

long long inv(long long x) { return bin_pow(x, P - 2); }

long long choose(long long n, long long k) {
  if (k > n) {
    return 0;
  }
  return ((fact[n] * inv_fact[k]) % P * inv_fact[n - k]) % P;
}

int main() {
  _upgrade;

  fact[0] = 1;
  for (int i = 1; i < 300004; i++) {
    fact[i] = (i * fact[i - 1]) % P;
  }
  for (int i = 0; i < 300004; i++) {
    inv_fact[i] = inv(fact[i]);
  }

  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long>> events;
  for (int i = 1; i <= n; i++) {
    long long L, R;
    cin >> L >> R;
    events.emplace_back(L, -i);
    events.emplace_back(R, i);
  }

  sort(events.begin(), events.end());

  long long nb_of_segments = 0;
  long long res = 0;
  for (auto &[pos, type] : events) {
    if (type < 0) {
      res += choose(nb_of_segments, k - 1);
      res %= P;
      nb_of_segments++;
    } else {
      nb_of_segments--;
    }
  }
  cout << res << "\n";
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init(int tree_size) {
    even_max.assign(2 * tree_size + 1, 0);
    even_min.assign(2 * tree_size + 1, 0);
    odd_max.assign(2 * tree_size + 1, 0);
    odd_min.assign(2 * tree_size + 1, 0);
  }

  void update(int v, int val, int target, int L, int R) {
    if (L == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (target <= M) {
      update(2 * v, val, target, L, M);
    } else {
      update(2 * v + 1, val, target, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

int calc_tree_size(int n) {
  int sz = 1;
  while (sz < n) {
    sz *= 2;
  }
  return sz;
}

void test_case() {
  int n, q;
  cin >> n >> q;

  SegmentTree tree;
  int tree_size = calc_tree_size(n);
  tree.init(tree_size);

  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i, 1, tree_size);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j, 1, tree_size);
    tree.update(1, A[j], i, 1, tree_size);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init(int tree_size) {
    even_max.assign(2 * tree_size, 0);
    even_min.assign(2 * tree_size, 0);
    odd_max.assign(2 * tree_size, 0);
    odd_min.assign(2 * tree_size, 0);
  }

  void update(int v, int val, int target, int L, int R) {
    if (L == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (target <= M) {
      update(2 * v, val, target, L, M);
    } else {
      update(2 * v + 1, val, target, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

int calc_tree_size(int n) {
  int sz = 1;
  while (sz < n) {
    sz *= 2;
  }
  return sz;
}

void test_case() {
  int n, q;
  cin >> n >> q;

  SegmentTree tree;
  int tree_size = calc_tree_size(n);
  tree.init(tree_size);

  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i, 1, tree_size);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j, 1, tree_size);
    tree.update(1, A[j], i, 1, tree_size);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

const int N = 2 * 300004;
struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init() {
    even_max.assign(N, 0);
    even_min.assign(N, 0);
    odd_max.assign(N, 0);
    odd_min.assign(N, 0);
  }

  void update(int v, int val, int target, int L = 1, int R = N / 2) {
    if (L == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (target <= M) {
      update(2 * v, val, target, L, M);
    } else {
      update(2 * v + 1, val, target, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

void test_case() {
  SegmentTree tree;
  tree.init();

  int n, q;
  cin >> n >> q;
  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j);
    tree.update(1, A[j], i);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

const int N = (1 << 18);
struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init() {
    even_max.assign(N, 0);
    even_min.assign(N, 0);
    odd_max.assign(N, 0);
    odd_min.assign(N, 0);
  }

  void update(int v, int val, int target, int L = 1, int R = N / 2) {
    if (L == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (target <= M) {
      update(2 * v, val, target, L, M);
    } else {
      update(2 * v + 1, val, target, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

void test_case() {
  SegmentTree tree;
  tree.init();

  int n, q;
  cin >> n >> q;
  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j);
    tree.update(1, A[j], i);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

struct SegmentTree {
  vector<long long> even_max;
  vector<long long> even_min;
  vector<long long> odd_max;
  vector<long long> odd_min;

  void init(int tree_size) {
    even_max.assign(2 * tree_size + 5, 0);
    even_min.assign(2 * tree_size + 5, 0);
    odd_max.assign(2 * tree_size + 5, 0);
    odd_min.assign(2 * tree_size + 5, 0);
  }

  void update(int v, int val, int target, int L, int R) {
    if (L == R) {
      even_max[v] = 0;
      even_min[v] = 0;
      odd_max[v] = val;
      odd_min[v] = val;
      return;
    }

    int M = (L + R) / 2;
    if (target <= M) {
      update(2 * v, val, target, L, M);
    } else {
      update(2 * v + 1, val, target, M + 1, R);
    }
    even_max[v] = max(even_max[2 * v] + even_max[2 * v + 1],
                      odd_max[2 * v] - odd_min[2 * v + 1]);
    even_min[v] = min(even_min[2 * v] + even_min[2 * v + 1],
                      odd_min[2 * v] - odd_max[2 * v + 1]);
    odd_max[v] = max(even_max[2 * v] + odd_max[2 * v + 1],
                     odd_max[2 * v] - even_min[2 * v + 1]);
    odd_min[v] = min(even_min[2 * v] + odd_min[2 * v + 1],
                     odd_min[2 * v] - even_max[2 * v + 1]);
  }
};

int calc_tree_size(int n) {
  int sz = 1;
  while (sz < n) {
    sz *= 2;
  }
  return sz;
}

void test_case() {
  int n, q;
  cin >> n >> q;

  SegmentTree tree;
  int tree_size = 2 * calc_tree_size(n);
  tree.init(tree_size);

  vector<int> A(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    tree.update(1, A[i], i, 1, tree_size);
  }

  cout << tree.even_max[1] << "\n";
  while (q--) {
    int i, j;
    cin >> i >> j;

    tree.update(1, A[i], j, 1, tree_size);
    tree.update(1, A[j], i, 1, tree_size);

    swap(A[i], A[j]);

    cout << tree.even_max[1] << "\n";
  }

  return;
}

int main() {
  _upgrade;
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        if(n == 1){
            for(int i = 1;i <= q + 1;i++)cout << ans << ' ';
            cout <<endl;
            continue;
        }
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<ll>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
//        if(n == 1){
//            for(int i = 1;i <= q + 1;i++)cout << ans << ' ';
//            cout <<endl;
//            continue;
//        }
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        if(n == 1){
            for(int i = 1;i <= q + 1;i++)cout << a[1] << ' ';
            cout <<endl;
            continue;
        }
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 1000010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        if(n == 1){
            for(int i = 1;i <= q + 1;i++)cout << a[1] << ' ';
            cout <<endl;
            continue;
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 1000010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<int>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0,a[i] - a[i+1]);
        ans += a[n];
        if(n == 1){
            for(int i = 1;i <= q + 1;i++)cout << a[1] << ' ';
            cout <<endl;
            continue;
        }
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << ' ';
        cout << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int a[maxn];
vector<ll>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0ll,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << endl;
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<unordered_map>
//#include<bits/stdc++.h>
#define fi first
#define se second
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define maxn 300010
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<ll, ll>
#define pi acos(-1)
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define lowbit(x) (x&(-x))
#define SZ(x) ((ll)(x).size())
#define met(a,x) memset(a,x,sizeof(a))
#define openin(x) freopen(x, "r", stdin)
#define openout(x) freopen(x, "w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define bep(i,a,b) for(int i = a;i >= b;i--)
#define ios() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int days[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leapyear(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline ll read() {
    ll x = 0, sign = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sign = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    return sign * x;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
ll a[maxn];
vector<ll>vec;
int main() {
    int T; cin >> T;
    while (T--) {
        vec.clear();
        int n,q;cin >> n >> q;
        rep(i,1,n)cin >> a[i];
        ll ans = 0;
        rep(i,1,n-1)ans += max(0ll,a[i] - a[i+1]);
        ans += a[n];
        vec.pb(ans);
        while(q--){
//            cout << ans << ' ';
            int l,r;cin >> l >> r;
            if(l == r){
                vec.pb(ans);
                continue;
            }
            if(l > r)swap(a[l],a[r]);
            if(l > 1)ans -= max(0,a[l-1]-a[l]);
            if(l < n)ans -= max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans -= max(0,a[r-1] - a[r]);
            if(r < n)ans -= max(0,a[r] - a[r+1]);
            if(r == n)ans -= a[n];
            swap(a[l],a[r]);
            if(l > 1)ans += max(0,a[l-1]-a[l]);
            if(l < n)ans += max(0,a[l] - a[l+1]);
            if(r > 1 && r > l + 1)ans += max(0,a[r-1] - a[r]);
            if(r < n)ans += max(0,a[r] - a[r+1]);
            if(r == n)ans += a[n];
            vec.pb(ans);
        }
        for(int i = 0;i < vec.size();i++)cout << vec[i] << endl;
    }
    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
inline void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

inline void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        insert(i);
    }

    cout << ans << nl;

    while(q--){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        insert(l-1);
        insert(l);
        insert(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) insert(r-l);
            if(r != l+1) insert(r);
            insert(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    else if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    else if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l < r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l < r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void insert(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        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 << nl;

    while(q--){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        insert(l-1);
        insert(l);
        insert(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) insert(r-l);
            if(r != l+1) insert(r);
            insert(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void insert(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
        cin>>n>>q;
        for (int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        a[0]=-1;
        a[n+1]=-1;
        ans=0;
        for (int i=1; i<=n; i++)
        {
            if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
            if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
        }
        cout<<ans<<'\n';
        while (q--)
        {
            int l,r;
            cin>>l>>r;
            erase(l-1);
            erase(l);
            erase(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) erase(r-1);
                if (r!=l+1) erase(r);
                erase(r+1);
            }
            swap(a[l],a[r]);
            insert(l-1);
            insert(l);
            insert(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) insert(r-1);
                if (r!=l+1) insert(r);
                insert(r+1);
            }
            cout<<ans<<'\n';
        }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void incld(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        incld(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        incld(l-1);
        incld(l);
        incld(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) incld(r-l);
            if(r != l+1) incld(r);
            incld(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void insert(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        insert(i);
    }

    cout << ans << nl;

    while(q--){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        insert(l-1);
        insert(l);
        insert(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) insert(r-l);
            if(r != l+1) insert(r);
            insert(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(1000000);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-1);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-1);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    while(q--){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    int q; cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void insert(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        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 << nl;

    while (q--)
        {
            int l,r;
            cin>>l>>r;
            erase(l-1);
            erase(l);
            erase(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) erase(r-1);
                if (r!=l+1) erase(r);
                erase(r+1);
            }
            swap(a[l],a[r]);
            insert(l-1);
            insert(l);
            insert(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) insert(r-1);
                if (r!=l+1) insert(r);
                insert(r+1);
            }
            cout<<ans<<'\n';
        }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l != r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void insert(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
        cin>>n>>q;
        for (int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        a[0]=-1;
        a[n+1]=-1;
        ans=0;
        for (int i=1; i<=n; i++)
        {
            if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
            if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
        }
        cout<<ans<<'\n';
        while (q--)
        {
            int l,r;
            cin>>l>>r;
            erase(l-1);
            erase(l);
            erase(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) erase(r-1);
                if (r!=l+1) erase(r);
                erase(r+1);
            }
            swap(a[l],a[r]);
            insert(l-1);
            insert(l);
            insert(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) insert(r-1);
                if (r!=l+1) insert(r);
                insert(r+1);
            }
            cout<<ans<<'\n';
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


#include<iostream>

using namespace std;

#define int long long

int a[1000000+5];

int n;

int ans=0;

inline void insert(int i)
{
    if (i==0||i==n+1) return;
    if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
    if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
}

inline void erase(int i)
{
    if (i==0||i==n+1) return;
    if (a[i-1]<a[i]&&a[i]>a[i+1]) ans-=a[i];
    if (a[i-1]>a[i]&&a[i]<a[i+1]) ans+=a[i];
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        int q;
        cin>>n>>q;
        for (int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        a[0]=-1;
        a[n+1]=-1;
        ans=0;
        for (int i=1; i<=n; i++)
        {
            if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
            if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
        }
        cout<<ans<<'\n';
        while (q--)
        {
            int l,r;
            cin>>l>>r;
            erase(l-1);
            erase(l);
            erase(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) erase(r-1);
                if (r!=l+1) erase(r);
                erase(r+1);
            }
            swap(a[l],a[r]);
            insert(l-1);
            insert(l);
            insert(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) insert(r-1);
                if (r!=l+1) insert(r);
                insert(r+1);
            }
            cout<<ans<<'\n';
        }
    }
}


// !!!!! DO NOT FORGET TO UNDEFINE LOCAL BEFORE SUBMISSION !!!!!
// #define LOCAL

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <iomanip>
#include <queue>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define forn(i, n) for(int i=0; i<n; i++)
#define formn(i, m, n) for(int i=m; i<=n; i++)
#define formnr(i, m, n) for(int i=m; i>=n; i--)

#define nl "\n"
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define fr first
#define sec second
#define ins insert
#define all(x) x.begin(), x.end()
#define re return

#define INF_INT 1e9
#define INF_LNG 1e18
#define PI  3.14159265

ll gcd(ll a, ll b){
    while(b > 0){a %= b; swap(a, b);}
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

vi a(300000+2);
int n, q;
ll ans;
void add(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans += a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans -= a[x];
}

void erase(int x){
    if(x == 0 || x == n+1) return;
    if(a[x] > a[x-1] && a[x] > a[x+1]) ans -= a[x];
    if(a[x] < a[x-1] && a[x] < a[x+1]) ans += a[x];
}

void solve(){
    cin >> n >> q;
    formn(i, 1, n) cin >> a[i];
    a[0] = -1;
    a[n+1] = -1;

    ans = 0;
    formn(i, 1, n){
        add(i);
    }

    cout << ans << nl;

    forn(i, q){
        int l, r; cin >> l >> r;
        erase(l-1);
        erase(l);
        erase(l+1);
        if(l < r){
            if(r-1 != l && r-1 != l+1) erase(r-l);
            if(r != l+1) erase(r);
            erase(r+1);
        }

        swap(a[l], a[r]);
        add(l-1);
        add(l);
        add(l+1);
        if(l < r){
            if(r-1 != l && r-1 != l+1) add(r-l);
            if(r != l+1) add(r);
            add(r+1);
        }

        cout << ans << nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int t; cin >> t;
    forn(i, t) solve();

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll an = 0;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i+1 >= a.size()) return 0;
    return max(0ll, a[i]-a[i+1]);
}

void update(vector<ll> &a, int i, int v)
{
    an -= calc(a, i);
    an -= calc(a, i-1);
    a[i] = v;
    an += calc(a, i);
    an += calc(a, i-1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;
        a.push_back(0);

        an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            /*
            set<int> indices = {l-1, l, r-1, r+1};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            */
            int al = a[l];
            update(a, l, a[r]);
            update(a, r, al);
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i >= a.size()) return 0;
    if (i == 0) return a[0];
    return max(0ll, a[i]-a[i-1]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;

        ll an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            vector<int> indices = {l-1, l, l+1, r-1, r, r+1};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            an = 0;
            for (int i = 0; i < a.size(); ++i) {
                an += calc(a, i);
            }
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i+1 >= a.size()) return 0;
    return max(0ll, a[i]-a[i+1]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;
        a.push_back(0);

        ll an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            set<int> indices = {l-1, l, l+1, r-1, r, r+1};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i >= a.size()) return 0;
    if (i == 0) return a[0];
    return max(0ll, a[i]-a[i-1]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;

        ll an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            vector<int> indices = {l-1, l, l+1, r-1, r, r+1};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i+1 >= a.size()) return 0;
    return max(0ll, a[i]-a[i+1]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;
        a.push_back(0);

        ll an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            set<int> indices = {l-1, l, r-1, r};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll an = 0;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i+1 >= a.size()) return 0;
    return max(0ll, a[i]-a[i+1]);
}

void update(vector<ll> &a, int i, int v)
{
    an -= calc(a, i);
    an -= calc(a, i-1);
    a[i] = v;
    an += calc(a, i);
    an += calc(a, i-1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;
        a.push_back(0);

        an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            int al = a[l];
            update(a, l, a[r]);
            update(a, r, al);
            cout << an << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll calc(const vector<ll> &a, int i)
{
    if (i < 0 || i >= a.size()) return 0;
    if (i == 0) return a[0];
    return max(0ll, a[i]-a[i-1]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<ll> a(n);
        for (auto &it: a) cin >> it;

        ll an = 0;
        for (int i = 0; i < a.size(); ++i) {
            an += calc(a, i);
        }
        cout << an << '\n';
        while (q--) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            set<int> indices = {l-1, l, l+1, r-1, r, r+1};
            for (int i: indices) {
                an -= calc(a, i);
            }
            swap(a[l], a[r]);
            for (int i: indices) {
                an += calc(a, i);
            }
            cout << an << '\n';
        }
    }
    return 0;
}


// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 5e5 + 47, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
struct node
{
  vector <vector <int>> dp;
  node()
  {
    dp.resize(2, vector <int>(2));
    dp[0][0] = -INF;
    dp[0][1] = -INF;
    dp[1][0] = -INF;
    dp[1][1] = -INF;
  }
};
node operator + (node a, node b)
{
  node cur;
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      cur.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
      for (int k = 0; k < 2; ++k)
        cur.dp[i][j] = max(cur.dp[i][j], a.dp[i][k] + b.dp[k ^ 1][j]);
    }
  return cur;
}
struct segment_tree
{
  vector <node> t;
  int n;
  segment_tree()
  {

  }
  segment_tree(int N)
  {
    t.resize(N * 4);
    n = N;
  }
  node make(int val)
  {
    node res;
    res.dp[0][0] = -val;
    res.dp[1][1] = val;
    res.dp[0][1] = -INF;
    res.dp[1][0] = -INF;
    return res;
  }
  void build(vector <int>& a, int l, int r, int v = 1)
  {
    if (l == r)
    {
      t[v] = make(a[l]);
      return;
    }
    int mid = l + r >> 1;
    build(a, l, mid, v * 2);
    build(a, mid + 1, r, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
    /*t[v].dp[0][0] = max({t[v * 2].dp[0][0], t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][1] + t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][0] + t[v * 2 + 1].dp[1][0]});
    t[v].dp[0][1] =
    t[v].dp[1][0] =
    t[v].dp[1][1] =*/
  }
  void update(int pos, int val, int tl, int tr, int v = 1)
  {
    if (tl > pos || tr < pos)
      return;
    if (tl == pos && tr == pos)
    {
      t[v] = make(val);
      return;
    }
    int mid = tl + tr >> 1;
    update(pos, val, tl, mid, v * 2);
    update(pos, val, mid + 1, tr, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
  void update(int pos, int val)
  {
    update(pos, val, 0, n - 1);
  }
};
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    cin >> a;
    segment_tree t(n);
    t.build(a, 0, n - 1);
    node cur = t.t[1];
    int ans = -INF;
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ans = max(ans, cur.dp[i][j]);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      --l, --r;
      t.update(l, a[r]);
      t.update(r, a[l]);
      swap(a[l], a[r]);
      int res = -INF;
      cur = t.t[1];
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          res = max(res, cur.dp[i][j]);
      cout << res << "\n";
    }
  }
}
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 3e5, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
struct node
{
  int dp[2][2];
};
node operator + (node& a, node& b)
{
  node cur;
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      cur.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
      for (int k = 0; k < 2; ++k)
        cur.dp[i][j] = max(cur.dp[i][j], a.dp[i][k] + b.dp[k ^ 1][j]);
    }
  return cur;
}
struct segment_tree
{
  vector <node> t;
  int n;
  segment_tree()
  {

  }
  segment_tree(int N)
  {
    t.resize(N * 4);
    n = N;
  }
  node make(int val)
  {
    node res;
    res.dp[0][0] = -val;
    res.dp[1][1] = val;
    res.dp[0][1] = -MEGAINF;
    res.dp[1][0] = -MEGAINF;
    return res;
  }
  void build(vector <int>& a, int l, int r, int v = 1)
  {
    if (l == r)
    {
      t[v] = make(a[l]);
      return;
    }
    int mid = l + r >> 1;
    build(a, l, mid, v * 2);
    build(a, mid + 1, r, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
    /*t[v].dp[0][0] = max({t[v * 2].dp[0][0], t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][1] + t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][0] + t[v * 2 + 1].dp[1][0]});
    t[v].dp[0][1] =
    t[v].dp[1][0] =
    t[v].dp[1][1] =*/
  }
  void update(int pos, int val, int tl, int tr, int v = 1)
  {
    if (tl > pos || tr < pos)
      return;
    if (tl == pos && tr == pos)
    {
      t[v] = make(val);
      return;
    }
    int mid = tl + tr >> 1;
    update(pos, val, tl, mid, v * 2);
    update(pos, val, mid + 1, tr, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
  void update(int pos, int val)
  {
    update(pos, val, 0, n - 1);
  }
};
//segment_tree tree(MAXN);
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    cin >> a;
    segment_tree tree(n);
    tree.build(a, 0, n - 1);
    node cur = tree.t[1];
    int ans = -MEGAINF;
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ans = max(ans, cur.dp[i][j]);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      --l, --r;
      tree.update(l, a[r]);
      tree.update(r, a[l]);
      swap(a[l], a[r]);
      ans = -MEGAINF;
      cur = tree.t[1];
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          ans = max(ans, cur.dp[i][j]);
      cout << ans << "\n";
    }
  }
}
/*
1
50 0
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
*/
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.