#include <bits/stdc++.h>   using namespace std;   long long mul(long long a, long long b, long long p = 1000000007) {     return ((a % p) * (b % p)) % p;   }   long long add(long long a, long long b, long long p = 1000000007) {     return (a % p + b % p) % p;   }   void input(long long a[], long long sz) {     for (long long i = 0; i < sz; i++) cin >> a[i];   }   void print(long long a[], long long sz) {     for (long long i = 0; i < sz; i++) {       if (i == sz - 1)         cout << a[i] <<   n ;       else         cout << a[i] <<    ;     }   }   long long maxr(long long a[], long long sz) {     long long ma;     for (long long i = 0; i < sz; i++) {       if (i == 0)         ma = a[i];       else if (a[i] > ma)         ma = a[i];     }     return ma;   }   long long minr(long long a[], long long sz) {     long long mi;     for (long long i = 0; i < sz; i++) {       if (i == 0)         mi = a[i];       else if (a[i] < mi)         mi = a[i];     }     return mi;   }   long long isprm(long long n) {     if (n <= 1) return 0;     if (n <= 3) return 1;     if (n % 2 == 0 || n % 3 == 0) return 0;     for (int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return 0;     return 1;   }   long long power(long long x, long long y, long long p = 1000000007) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long modInverse(long long n, long long p = 1000000007) {     return power(n, p - 2, p);   }   long long ncrMod(long long n, long long r, long long p = 1000000007) {     if (r == 0) return 1;     long long fac[n + 1];     fac[0] = 1;     for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;     return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %            p;   }   struct comp {     bool operator()(const pair<long long, long long> &a,                     const pair<long long, long long> &b) const {       if (a.first == b.first) {         return (a.second > b.second);       }       return (a.first < b.first);     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     cin >> t;     while (t--) {       long long n, q;       cin >> n >> q;       long long a[n + 5];       input(a, n);       if (n == 1) {         cout << a[0] <<   n ;         for (long long i = 0; i < q; i++) {           long long l, r;           cin >> l >> r;           cout << a[0] <<   n ;         }         continue;       }       map<long long, long long> mp;       long long ans = 0;       long long k = 0;       for (long long i = 0; i < n; i++) {         if (k % 2 == 0) {           if ((i - 1) >= 0 && (i + 1) < n && a[i] > a[i - 1] && a[i] > a[i + 1]) {             ans += a[i];             mp[i] = 1;             k++;           } else if ((i - 1) < 0 && a[i] > a[i + 1]) {             ans += a[i];             mp[i] = 1;             k++;           } else if ((i + 1) >= n && a[i] > a[i - 1]) {             ans += a[i];             mp[i] = 1;             k++;           }         } else {           if ((i - 1) >= 0 && (i + 1) < n && a[i] < a[i - 1] && a[i] < a[i + 1]) {             ans -= a[i];             mp[i] = -1;             k++;           }         }       }       if (k % 2 == 0) {         auto it = mp.end();         it--;         long long i = (*it).first;         ans += a[i];         mp.erase(it);       }       cout << ans <<   n ;       for (long long i = 0; i < q; i++) {         long long l, r;         cin >> l >> r;         l--;         r--;         long long x = max(l - 1, (long long)0);         long long y = min(l + 1, n - 1);         for (long long i = x; i < y + 1; i++) {           if (mp[i] == 1)             ans -= a[i];           else if (mp[i] == -1)             ans += a[i];           else             mp.erase(i);         }         x = max(r - 1, y + 1);         y = min(r + 1, n - 1);         for (long long i = x; i < y + 1; i++) {           if (mp[i] == 1)             ans -= a[i];           else if (mp[i] == -1)             ans += a[i];           else             mp.erase(i);         }         long long t = a[l];         a[l] = a[r];         a[r] = t;         x = max(l - 1, (long long)0);         y = min(l + 1, n - 1);         long long h;         auto it = mp.lower_bound(x);         if (it == mp.end()) {           it--;           h = (*it).second;           if (h == 1)             h = -1;           else             h = 1;         } else {           h = (*it).second;         }         for (long long i = x; i < y + 1; i++) {           if (h == 1) {             if ((i - 1) >= 0 && (i + 1) < n && a[i] > a[i - 1] &&                 a[i] > a[i + 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else if ((i - 1) < 0 && a[i] > a[i + 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else if ((i + 1) >= n && a[i] > a[i - 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else               mp.erase(i);           } else {             if ((i - 1) >= 0 && (i + 1) < n && a[i] < a[i - 1] &&                 a[i] < a[i + 1]) {               ans -= a[i];               mp[i] = -1;               h = 1;             } else               mp.erase(i);           }         }         x = max(r - 1, y + 1);         y = min(r + 1, n - 1);         auto it2 = mp.lower_bound(x);         if (it2 == mp.begin()) {           h = 1;         } else {           it2--;           h = (*it2).second;           if (h == 1)             h = -1;           else             h = 1;         }         for (long long i = x; i < y + 1; i++) {           if (h == 1) {             if ((i - 1) >= 0 && (i + 1) < n && a[i] > a[i - 1] &&                 a[i] > a[i + 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else if ((i - 1) < 0 && a[i] > a[i + 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else if ((i + 1) >= n && a[i] > a[i - 1]) {               ans += a[i];               mp[i] = 1;               h = -1;             } else               mp.erase(i);           } else {             if ((i - 1) >= 0 && (i + 1) < n && a[i] < a[i - 1] &&                 a[i] < a[i + 1]) {               ans -= a[i];               mp[i] = -1;               h = 1;             } else               mp.erase(i);           }         }         if (mp.size() % 2 == 0 && mp.size() > 0) {           auto it = mp.end();           it--;           long long i = (*it).first;           ans += a[i];           mp.erase(it);         }         cout << ans <<   n ;       }     }     return 0;   }