#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(lli x){
    if(x == 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x == n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x +1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(lli x){
    if(x < n-1){
        if(maxm.count(x+2)){
            maxm.erase(x+2);
            nax -= arr[x+1];
        }
        if(minm.count(x+2)){
            minm.erase(x+2);
            nax += arr[x+1];
        }
    }
    if(x > 0){
        if(maxm.count(x)){
            maxm.erase(x);
            nax -= arr[x-1];
        }
        if(minm.count(x)){
            minm.erase(x);
            nax += arr[x-1];
        }
    }
    if(maxm.count(x+1)){
        maxm.erase(x+1);
        nax -= arr[x];
    }
    if(minm.count(x+1)){
        minm.erase(x+1);
        nax += arr[x];
    }
}
void brute(){
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    lli i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        flip *= -1;
    }
    cout<<max<<endl;
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        return;
    }
    int flip = 1;
    lli sum = 0;
    int i = 1;
    for(int i = 1; i<n; i++){
        while(i < n && flip*(arr[i]-arr[i-1]) > 0)
            i++;
        sum = sum + flip*arr[i-1];
        if(flip+1){
            maxm.insert(i);
            nax = sum;
        }
        else
            minm.insert(i);
        flip *= -1;
    }
    if(minm.size() == maxm.size())
        minm.erase(*minm.rbegin());
    cout<<nax<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r)
            continue;
        del(l-1);
        del(r-1);
        l--,r--;
       // debug(minm,maxm);
        swap(arr[l], arr[r]);
        set <lli> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        if(l < n-1)
            check.insert(l+1);
        if(r > 0)
            check.insert(r-1);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            if(i == 710){
                cout<<l<<" "<<r<<endl;
            }
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(check);
        //debug(nax,minm,maxm);
        cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
   // t = 1;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(lli x){
    if(x <= 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x >= n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x+1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(lli x){
    if(maxm.count(x+1)){
        nax -= arr[x];
        maxm.erase(x+1);
    }
    if(minm.count(x+1)){
        nax += arr[x];
        minm.erase(x+1);
    }
}
void brute(){
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    lli i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        flip *= -1;
    }
    cout<<max<<endl;
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        nax = 1;
    }
    else{
int flip = 1;
    lli sum = 0;
    lli max = -1;
    int i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        if(i == n){
            if(arr[i-1] > arr[i-2])
                maxm.insert(n);
        }
        else if(i == 1){
            if(arr[0] > arr[1])
                maxm.insert(1);
        }
        else{
            if(arr[i-1] > arr[i] && arr[i-1] > arr[i-2])
                maxm.insert(i);
            if(arr[i-1] < arr[i] && arr[i-1] < arr[i-2])
                minm.insert(i);
        }
       // debug(maxm,minm);
        flip *= -1;
    }
    nax = max;
    cout<<max<<endl;
    }

    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r || n == 1){
            cout<<nax<<endl;
            continue;
        }
        l--,r--;
        set <lli> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        if(l < n -1)
            check.insert(l+1);
        if(r > 1)
            check.insert(r-1);
        for(auto i : check)
            del(i);
       // debug(check,maxm,minm,nax);
        swap(arr[l], arr[r]);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(nax,minm,maxm);

            cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    t = 10000;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(lli x){
    if(x <= 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x >= n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x+1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(lli x){
    if(maxm.count(x+1)){
        nax -= arr[x];
        maxm.erase(x+1);
    }
    if(minm.count(x+1)){
        nax += arr[x];
        minm.erase(x+1);
    }
}
void brute(){
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    lli i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        flip *= -1;
    }
    cout<<max<<endl;
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        return;
    }
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    int i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        if(i == n){
            if(arr[i-1] > arr[i-2])
                maxm.insert(n);
        }
        else if(i == 1){
            if(arr[0] > arr[1])
                maxm.insert(1);
        }
        else{
            if(arr[i-1] > arr[i] && arr[i-1] > arr[i-2])
                maxm.insert(i);
            if(arr[i-1] < arr[i] && arr[i-1] < arr[i-2])
                minm.insert(i);
        }
       // debug(maxm,minm);
        flip *= -1;
    }
    nax = max;
    cout<<max<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r){
            cout<<nax<<endl;
            continue;
        }
        l--,r--;
        set <lli> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        if(l < n -1)
            check.insert(l+1);
        if(r > 1)
            check.insert(r-1);
        for(auto i : check)
            del(i);
       // debug(check,maxm,minm,nax);
        swap(arr[l], arr[r]);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(nax,minm,maxm);

            cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    t = 10000;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(int x){
    if(x == 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x == n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x +1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(int x){
    if(x < n-1){
        if(maxm.count(x+2)){
            maxm.erase(x+2);
            nax -= arr[x+1];
        }
        if(minm.count(x+2)){
            minm.erase(x+2);
            nax += arr[x+1];
        }
    }
    if(x > 0){
        if(maxm.count(x)){
            maxm.erase(x);
            nax -= arr[x-1];
        }
        if(minm.count(x)){
            minm.erase(x);
            nax += arr[x-1];
        }
    }
    if(maxm.count(x+1)){
        maxm.erase(x+1);
        nax -= arr[x];
    }
    if(minm.count(x+1)){
        minm.erase(x+1);
        nax += arr[x];
    }
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        return;
    }
    int flip = 1;
    lli sum = 0;
    int i = 1;
    for(int i = 1; i<n; i++){
        while(i < n && flip*(arr[i]-arr[i-1]) > 0)
            i++;
        sum = sum + flip*arr[i-1];
        if(flip+1){
            maxm.insert(i);
            nax = sum;
        }
        else
            minm.insert(i);
        flip *= -1;
    }
    if(minm.size() == maxm.size()){
        minm.erase(*minm.rbegin());
    }
    cout<<nax<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r)
            continue;
        del(l-1);
        del(r-1);
        l--,r--;
       // debug(minm,maxm);
        swap(arr[l], arr[r]);
        set <int> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        if(l < n-1)
            check.insert(l+1);
        if(r > 0)
            check.insert(r-1);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            assert(0);
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(check);
        //debug(nax,minm,maxm);
        cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
   // t = 1;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
typedef long long int lli;
#define MOD 1000000007
#define MAX 2000005
#define insert push_back
#define endl "\n"
lli sum = 0;
lli arr[MAX];
void del(int x){
    if(arr[x] > arr[x-1] & arr[x] > arr[x+1])
         sum -= arr[x];
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1])
         sum += arr[x];
}
void ins(lli x){
    if(arr[x] > arr[x-1] & arr[x] > arr[x+1])
        sum += arr[x];
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1])
        sum -= arr[x];
}
void solve(){
    lli n,q;
    cin>>n>>q;
    sum = 0;
    arr[0] = -1;
    arr[n+1] = -1;
    for(lli i = 1; i<=n; i++)
        cin>>arr[i];
    int flag = 0;
    //lli sum = 0;
    for(lli i = 1; i<=n; i++){
        if(!flag & arr[i] -arr[i-1] > 0 &&  arr[i] - arr[i+1] > 0){
            sum += arr[i];
            flag = !flag;
        }
        if(flag & arr[i] - arr[i-1] < 0 && arr[i] - arr[i+1] < 0){
            sum -= arr[i];
            flag = !flag;
        }
    }
    cout<<sum<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r){
            cout<<sum<<endl;
            continue;
        }
        vector <lli> check;
        if(l > 1)
            check.insert(l-1);
        check.insert(l);
        check.insert(l+1);
        if(r-1 != l+1 && r-1 != l)
            check.insert(r-1);
        if(r != l+1)
            check.insert(r);
        if(r < n)
            check.insert(r+1);
        //debug(check);
        for(auto i : check)
            del(i);
       // debug(sum);
        swap(arr[l],arr[r]);
        for(auto i : check)
            ins(i);
        cout<<sum<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    cin>>t;
    while(t--)solve();

}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(lli x){
    if(x == 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x == n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x +1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(lli x){
    if(maxm.count(x+1)){
        nax -= arr[x];
        maxm.erase(x+1);
    }
    if(minm.count(x+1)){
        nax += arr[x];
        minm.erase(x+1);
    }
}
void brute(){
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    lli i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        flip *= -1;
    }
    cout<<max<<endl;
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        return;
    }
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    int i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        if(i == n){
            if(arr[i-1] > arr[i-2])
                maxm.insert(n);
        }
        else if(i == 1){
            if(arr[0] > arr[1])
                maxm.insert(1);
        }
        else{
            if(arr[i-1] > arr[i] && arr[i-1] > arr[i-2])
                maxm.insert(i);
            if(arr[i-1] < arr[i] && arr[i-1] < arr[i-2])
                minm.insert(i);
        }
       // debug(maxm,minm);
        flip *= -1;
    }
    nax = max;
    cout<<max<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r)
            continue;
        l--,r--;
        set <lli> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        check.insert(l+1);
        check.insert(r-1);
        for(auto i : check)
            del(i);
       // debug(check,maxm,minm,nax);
        swap(arr[l], arr[r]);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(nax,minm,maxm);
        cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    t = 10000;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 998244353
#define MAX 500005
#define endl "\n"
lli arr[MAX];
lli n;
lli nax = -1;
set<lli> maxm;
set <lli> minm;
void subprocess(lli x){
    if(x == 0){
        if(arr[0] > arr[1]){
            maxm.insert(1);
            nax += arr[0];
        }
        return;
    }
    if(x == n-1){
        if(arr[n-1] > arr[n-2]){
            maxm.insert(n);
            nax += arr[n-1];
        }
        return;
    }
    assert(x > 0 && x +1 < n);
    if(arr[x]> arr[x-1] && arr[x] > arr[x+1]){
        maxm.insert(x+1);
        nax += arr[x];
    }
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1]){
        minm.insert(x+1);
        nax -= arr[x];
    }
}
void del(lli x){
    if(x < n-1){
        if(maxm.count(x+2)){
            maxm.erase(x+2);
            nax -= arr[x+1];
        }
        if(minm.count(x+2)){
            minm.erase(x+2);
            nax += arr[x+1];
        }
    }
    if(x > 0){
        if(maxm.count(x)){
            maxm.erase(x);
            nax -= arr[x-1];
        }
        if(minm.count(x)){
            minm.erase(x);
            nax += arr[x-1];
        }
    }
    if(maxm.count(x+1)){
        maxm.erase(x+1);
        nax -= arr[x];
    }
    if(minm.count(x+1)){
        minm.erase(x+1);
        nax += arr[x];
    }
}
void brute(){
    int flip = 1;
    lli sum = 0;
    lli max = -1;
    lli i = 1;
    while(i < n){
        while(i < n && flip*(arr[i]-arr[i-1]) >= 0)
            i++;
         sum = sum + arr[i-1]*flip;
        if(flip+1)
            max = sum;
        flip *= -1;
    }
    cout<<max<<endl;
}
void solve(){
    nax = -1;
    maxm.clear();
    minm.clear();
    lli q;
    cin>>n>>q;
    for(int i = 0; i<n; i++)
        cin>>arr[i];
    if(n == 1){
        cout<<arr[0]<<endl;
        return;
    }
    int flip = 1;
    lli sum = 0;
    int i = 1;
    for(int i = 1; i<n; i++){
        while(i < n && flip*(arr[i]-arr[i-1]) > 0)
            i++;
        sum = sum + flip*arr[i-1];
        if(flip+1){
            maxm.insert(i);
            nax = sum;
        }
        else
            minm.insert(i);
        flip *= -1;
    }
    if(minm.size() == maxm.size())
        minm.erase(*minm.rbegin());
    cout<<nax<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r)
            continue;
        del(l-1);
        del(r-1);
        l--,r--;
       // debug(minm,maxm);
        swap(arr[l], arr[r]);
        set <lli> check;
        if(l > 0)
            check.insert(l-1);
        if(r < n-1)
            check.insert(r+1);
        check.insert(l);
        check.insert(r);
        if(l < n-1)
            check.insert(l+1);
        if(r > 0)
            check.insert(r-1);
        for(auto i: check)
            subprocess(i);
        if(minm.size() == maxm.size()){
            cout<<"LINE NUMBERR"<<endl;
            nax += *minm.rbegin();
            minm.erase(*minm.rbegin());
        }
       // debug(check);
        //debug(nax,minm,maxm);
        cout<<nax<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
   // t = 1;
    cin>>t;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
#pragma region DEBUG
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
typedef long long int lli;
#define MOD 1000000007
#define MAX 2000005
#define endl "\n"
lli sum = 0;
lli arr[MAX];
void del(int x){
    if(arr[x] > arr[x-1] & arr[x] > arr[x+1])
         sum -= arr[x];
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1])
         sum += arr[x];
}
void ins(lli x){
    if(arr[x] > arr[x-1] & arr[x] > arr[x+1])
        sum += arr[x];
    if(arr[x] < arr[x-1] && arr[x] < arr[x+1])
        sum -= arr[x];
}
void solve(){
    lli n,q;
    cin>>n>>q;
    sum = 0;
    arr[0] = -1;
    arr[n+1] = -1;
    for(lli i = 1; i<=n; i++)
        cin>>arr[i];
    int flag = 0;
    //lli sum = 0;
    for(lli i = 1; i<=n; i++){
        if(!flag & arr[i] -arr[i-1] > 0 &&  arr[i] - arr[i+1] > 0){
            sum += arr[i];
            flag = !flag;
        }
        if(flag & arr[i] - arr[i-1] < 0 && arr[i] - arr[i+1] < 0){
            sum -= arr[i];
            flag = !flag;
        }
    }
    cout<<sum<<endl;
    for(lli i = 0; i<q; i++){
        lli l,r;
        cin>>l>>r;
        if(l == r){
            cout<<sum<<endl;
            continue;
        }
        set <lli> check;
        if(l > 1)
            check.insert(l-1);
        check.insert(l);
        check.insert(l+1);
        check.insert(r-1);
        check.insert(r);
        if(r < n)
            check.insert(r+1);
        //debug(check);
        for(auto i : check)
            del(i);
       // debug(sum);
        swap(arr[l],arr[r]);
        for(auto i : check)
            ins(i);
        cout<<sum<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t;
    cin>>t;
    while(t--)solve();

}


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

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 3e5 + 5;
using ll = long long;

int a[N];
ll dp[2][N<<1];

void assign(ll* t, int i, int x) {
    t[i+=N] = x;
    for (i>>=1; i > 0; i>>=1)
        t[i] = max(t[i<<1], t[i<<1|1]);
}

ll query(ll* t, int l, int r) {
    ll ans = 0;
    for (l+=N, r+=N; l < r; l>>=1, r>>=1) {
        if (l&1) ans = max(ans, t[l++]);
        if (r&1) ans = max(ans, t[--r]);
    }
    return ans;
}

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

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = n-1; i >= 0; i--) {
        assign(dp[0], i, a[i] + query(dp[1], i+1, n));
        assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
    }

    cout << query(dp[0], 0, n) << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;

        swap(a[l], a[r]);
        for (int i = r; i >= l; i--) {
            assign(dp[0], i, a[i] + query(dp[1], i+1, n));
            assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
        }

        cout << query(dp[0], 0, n) << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--)
        solve();

    return 0;
%>


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

// SecondThread explains this idea at
// https://www.youtube.com/watch?v=9YEQRwCtsnI&t=5577s

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 1e5 + 5;
using ll = long long;

int a[N];
int assign(int i, int x) {
    int delta = 0;

    delta -= max(0, a[i] - a[i-1]);
    delta -= max(0, a[i+1] - a[i]);

    a[i] = x;
    delta += max(0, a[i] - a[i-1]);
    delta += max(0, a[i+1] - a[i]);

    return delta;
}

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

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    a[n+1] = 0;

    ll ans = 0;
    for (int i = 1; i <= n; i++)
        ans += max(a[i] - a[i-1], 0);
    cout << ans << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        int ar = a[r];
        ans += assign(r, a[l]);
        ans += assign(l, ar);
        cout << ans << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--) {
        solve();
    }

    return 0;
%>


// https://codeforces.com/contest/1420/submission/93739425
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define endl '\n'
#define FASTIO std::ios::sync_with_stdio(false);

const int N = 3e5 + 50;

int a[N];
int n, q;
ll ans = 0;

inline void change(int idx, int v) {
  ans -= a[0];

  if (idx > 0) ans -= max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans -= max(0, a[idx + 1] - a[idx]);

  a[idx] = v;

  if (idx > 0) ans += max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans += max(0, a[idx + 1] - a[idx]);

  ans += a[0];
}

int(((((((((((((((main)))))))))))))))() {
  FASTIO;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> q;

    ans = 0;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      change(i, x);
    }
    cout << ans << endl;

    for (int i = 0; i < q; i++) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int t = a[r];
      change(r, a[l]);
      change(l, t);
      cout << ans << endl;
    }
  }
}


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

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 3e5 + 5;
using ll = long long;

int a[N];
ll dp[2][N<<1];

void assign(ll* t, int i, int x) {
    t[i+=N] = x;
    for (i>>=1; i > 0; i>>=1)
        t[i] = max(t[i<<1], t[i<<1|1]);
}

ll query(ll* t, int l, int r) {
    ll ans = 0;
    for (l+=N, r+=N; l < r; l>>=1, r>>=1) {
        if (l&1) ans = max(ans, t[l++]);
        if (r&1) ans = max(ans, t[--r]);
    }
    return ans;
}

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

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = n-1; i >= 0; i--) {
        assign(dp[0], i, a[i] + query(dp[1], i+1, n));
        assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
    }

    cout << query(dp[0], 0, n) << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;

        swap(a[l], a[r]);

        for (int i = r; i >= l; i--) {
            int last0 = query(dp[0], i, i+1);
            int last1 = query(dp[1], i, i+1);
            assign(dp[0], i, a[i] + query(dp[1], i+1, n));
            assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
            if (query(dp[0], i, i+1) == last0 && query(dp[1], i, i+1) == last1)
                break;
        }

        cout << query(dp[0], 0, n) << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--)
        solve();

    return 0;
%>


// https://codeforces.com/contest/1420/submission/93739425
#include <bits/stdc++.h>

#define endl '\n'
#define FASTIO std::ios::sync_with_stdio(false);

const int N = 3e5 + 50;

int a[N];
int n, q;
ll ans = 0;

inline void change(int idx, int v) {
  ans -= a[0];

  if (idx > 0) ans -= max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans -= max(0, a[idx + 1] - a[idx]);

  a[idx] = v;

  if (idx > 0) ans += max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans += max(0, a[idx + 1] - a[idx]);

  ans += a[0];
}

int(((((((((((((((main)))))))))))))))() {
  FASTIO;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> q;

    ans = 0;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      change(i, x);
    }
    cout << ans << endl;

    for (int i = 0; i < q; i++) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int t = a[r];
      change(r, a[l]);
      change(l, t);
      cout << ans << endl;
    }
  }
}


// https://codeforces.com/contest/1420/submission/93739425
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define endl '\n'
#define FASTIO std::ios::sync_with_stdio(false); cin.tie(0);

const int N = 3e5 + 50;

int a[N];
int n, q;
ll ans = 0;

inline void change(int idx, int v) {
  ans -= a[0];

  if (idx > 0) ans -= max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans -= max(0, a[idx + 1] - a[idx]);

  a[idx] = v;

  if (idx > 0) ans += max(0, a[idx] - a[idx - 1]);
  if (idx < n - 1) ans += max(0, a[idx + 1] - a[idx]);

  ans += a[0];
}

int(((((((((((((((main)))))))))))))))() {
  FASTIO;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> q;

    ans = 0;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      change(i, x);
    }
    cout << ans << endl;

    for (int i = 0; i < q; i++) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int t = a[r];
      change(r, a[l]);
      change(l, t);
      cout << ans << endl;
    }
  }
}


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

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 3e5 + 5;
using ll = long long;

int a[N];
ll dp[2][N<<1];

void assign(ll* t, int i, int x) {
    t[i+=N] = x;
    for (i>>=1; i > 0; i>>=1)
        t[i] = max(t[i<<1], t[i<<1|1]);
}

ll query(ll* t, int l, int r) {
    ll ans = 0;
    for (l+=N, r+=N; l < r; l>>=1, r>>=1) {
        if (l&1) ans = max(ans, t[l++]);
        if (r&1) ans = max(ans, t[--r]);
    }
    return ans;
}

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

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = n-1; i >= 0; i--) {
        assign(dp[0], i, a[i] + query(dp[1], i+1, n));
        assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
    }

    cout << query(dp[0], 0, n) << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;

        if (query(dp[1], r+1, n) + a[l] == query(dp[1], r, n)) {
            cout << query(dp[0], 0, n);
            continue;
        }

        swap(a[l], a[r]);

        for (int i = r; i >= l; i--) {
            assign(dp[0], i, a[i] + query(dp[1], i+1, n));
            assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
        }

        cout << query(dp[0], 0, n) << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--)
        solve();

    return 0;
%>


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

// SecondThread explains this idea at
// https://www.youtube.com/watch?v=9YEQRwCtsnI&t=5577s

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 3e5 + 5;
using ll = long long;

int a[N];
int assign(int i, int x) {
    int delta = 0;

    delta -= max(0, a[i] - a[i-1]);
    delta -= max(0, a[i+1] - a[i]);

    a[i] = x;
    delta += max(0, a[i] - a[i-1]);
    delta += max(0, a[i+1] - a[i]);

    return delta;
}

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

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    a[n+1] = 0;

    ll ans = 0;
    for (int i = 1; i <= n; i++)
        ans += max(a[i] - a[i-1], 0);
    cout << ans << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        int ar = a[r];
        ans += assign(r, a[l]);
        ans += assign(l, ar);
        cout << ans << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--) {
        solve();
    }

    return 0;
%>


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

#ifdef DEBUG
#define debug(...) __VA_ARGS__
#else
#define endl '\n'
#define debug(...)
#endif
#define debugf(...) debug(printf(__VA_ARGS__))

const int N = 3e5 + 5;
using ll = long long;

int a[N];
ll dp[2][N<<1];

void assign(ll* t, int i, int x) {
    t[i+=N] = x;
    for (i>>=1; i > 0; i>>=1)
        t[i] = max(t[i<<1], t[i<<1|1]);
}

ll query(ll* t, int l, int r) {
    ll ans = 0;
    for (l+=N, r+=N; l < r; l>>=1, r>>=1) {
        if (l&1) ans = max(ans, t[l++]);
        if (r&1) ans = max(ans, t[--r]);
    }
    return ans;
}

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

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = n-1; i >= 0; i--) {
        assign(dp[0], i, a[i] + query(dp[1], i+1, n));
        assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
    }

    cout << query(dp[0], 0, n) << endl;

    while (q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;

        if (query(dp[1], r+1, n) + a[l] == query(dp[1], r, n) &&
            query(dp[0], r+1, n) - a[l] == query(dp[0], r, n)) {
                cout << query(dp[0], 0, n);
                continue;
            }

        swap(a[l], a[r]);

        for (int i = r; i >= l; i--) {
            assign(dp[0], i, a[i] + query(dp[1], i+1, n));
            assign(dp[1], i, -a[i] + query(dp[0], i+1, n));
        }

        cout << query(dp[0], 0, n) << endl;
    }
}

int (((((main)))))() <%
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--)
        solve();

    return 0;
%>


/*input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			for(int i=0; i<=q; i++)
				cout << a[0] << "\n";
			continue;
		}
		bool maksimumas[n], minimumas[n];

		for(int i=0; i<n; i++)
		{
			minimumas[i] = false;
			maksimumas[i] = false;
		}

		long long ats = 0;

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		auto prisiminti = [&](int num)
		{
			if(num==0)
			{
				if(a[0]>a[1])
				{
					ats += a[0];
					maksimumas[0] = true;
				}
			}
			else if(num==n-1)
			{
				if(a[n-1]>a[n-2])
				{
					ats += a[n-1];
					maksimumas[n-1] = true;
				}
			}
			else
			{
				if((a[num-1]<a[num]) && (a[num]>a[num+1]))
				{
					ats += a[num];
					maksimumas[num] = true;
				}
				if((a[num-1]>a[num]) && (a[num]<a[num+1]))
				{
					ats -= a[num];
					minimumas[num] = true;
				}
			}
		};

		for(int i=0; i<n; i++)
		{
			prisiminti(i);
		}

		cout << ats << "\n";

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
				pamirsti(i);

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
				pamirsti(i);

			while((l<0) || (r>=(int)a.size()))
			{
				id++;
			}
			swap(a[l], a[r]);

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}
			cout << ats << "\n";
		}
	}
	return 0;
}


/*input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			for(int i=0; i<=q; i++)
				cout << a[0] << "\n";
			continue;
		}
		bool maksimumas[n], minimumas[n];

		long long ats = 0;

		for(int i=0; i<n; i++)
		{
			maksimumas[i] = false;
			minimumas[i] = false;
		}
		if(a[0]>a[1])
			maksimumas[0] = true;

		if(a[n-1]>a[n-2])
			maksimumas[n-1] = true;
		for(int i=1; i<n-1; i++)
		{
			if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				maksimumas[i] = true;
			if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				minimumas[i] = true;
		}
		for(int i=0; i<n; i++)
		{
			if(maksimumas[i])
				ats += a[i];
			if(minimumas[i])
				ats -= a[i];
		}

		/*cout << "pries viska " << endl;
		for(int i=0; i<n; i++)
			cout << a[i] << " ";
		cout << endl;
		for(int i=0; i<n; i++)
			cout << maksimumas[i] << " ";
		cout << endl;
		for(int i=0; i<n; i++)
			cout << minimumas[i] << " ";
		cout << endl;*/

		cout << ats << "\n";

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(1, l-5); i<=min(n-2, l+5); i++)
			{
				pamirsti(i);
			}

			for(int i=max(1, r-5); i<=min(n-2, r+5); i++)
			{
				pamirsti(i);
			}

			pamirsti(0);

			pamirsti(n-1);

			//cout << "nueimiau krastus: " << ats << endl;

			swap(a[l], a[r]);

			if(a[0]>a[1])
			{
				ats += a[0];
				maksimumas[0] = true;
			}

			if(a[n-1]>a[n-2])
			{
				ats += a[n-1];
				maksimumas[n-1] = true;
			}
			//cout << "dabar ats: " << ats << endl;

			for(int i=max(1, l-5); i<=min(n-2, l+5); i++)
			{
				pamirsti(i);
				if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				{
					ats += a[i];
					maksimumas[i] = true;
				}
				if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				{
					ats -= a[i];
					minimumas[i] = true;
				}
			}

			for(int i=max(1, r-5); i<=min(n-2, r+5); i++)
			{
				pamirsti(i);
				if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				{
					ats += a[i];
					maksimumas[i] = true;
				}
				if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				{
					ats -= a[i];
					minimumas[i] = true;
				}
			}
			/*cout << "po visko " << endl;
			for(int i=0; i<n; i++)
				cout << a[i] << " ";
			cout << endl;
			for(int i=0; i<n; i++)
				cout << maksimumas[i] << " ";
			cout << endl;
			for(int i=0; i<n; i++)
				cout << minimumas[i] << " ";
			cout << endl;*/
			cout << ats << "\n";
		}
		//cout << "kitas testas" << endl;
	}
	return 0;
}


/*input
1
1 0
1
1 1
1
1 1
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			for(int i=0; i<=q; i++)
				cout << a[0] << "\n";
			continue;
		}
		bool maksimumas[n], minimumas[n];

		long long ats = 0;

		for(int i=0; i<n; i++)
		{
			maksimumas[i] = false;
			minimumas[i] = false;
		}
		if(a[0]>a[1])
			maksimumas[0] = true;

		if(a[n-1]>a[n-2])
			maksimumas[n-1] = true;
		for(int i=1; i<n-1; i++)
		{
			if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				maksimumas[i] = true;
			if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				minimumas[i] = true;
		}
		for(int i=0; i<n; i++)
		{
			if(maksimumas[i])
				ats += a[i];
			if(minimumas[i])
				ats -= a[i];
		}

		/*cout << "pries viska " << endl;
		for(int i=0; i<n; i++)
			cout << a[i] << " ";
		cout << endl;
		for(int i=0; i<n; i++)
			cout << maksimumas[i] << " ";
		cout << endl;
		for(int i=0; i<n; i++)
			cout << minimumas[i] << " ";
		cout << endl;*/

		cout << ats << "\n";

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(1, l-5); i<=min(n-2, l+5); i++)
			{
				pamirsti(i);
			}

			for(int i=max(1, r-5); i<=min(n-2, r+5); i++)
			{
				pamirsti(i);
			}

			pamirsti(0);

			pamirsti(n-1);

			//cout << "nueimiau krastus: " << ats << endl;

			swap(a[l], a[r]);

			if(a[0]>a[1])
			{
				ats += a[0];
				maksimumas[0] = true;
			}

			if(a[n-1]>a[n-2])
			{
				ats += a[n-1];
				maksimumas[n-1] = true;
			}
			//cout << "dabar ats: " << ats << endl;

			for(int i=max(1, l-5); i<=min(n-2, l+5); i++)
			{
				pamirsti(i);
				if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				{
					ats += a[i];
					maksimumas[i] = true;
				}
				if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				{
					ats -= a[i];
					minimumas[i] = true;
				}
			}

			for(int i=max(1, r-5); i<=min(n-2, r+5); i++)
			{
				pamirsti(i);
				if((a[i-1]<a[i]) && (a[i]>a[i+1]))
				{
					ats += a[i];
					maksimumas[i] = true;
				}
				if((a[i-1]>a[i]) && (a[i]<a[i+1]))
				{
					ats -= a[i];
					minimumas[i] = true;
				}
			}
			/*cout << "po visko " << endl;
			for(int i=0; i<n; i++)
				cout << a[i] << " ";
			cout << endl;
			for(int i=0; i<n; i++)
				cout << maksimumas[i] << " ";
			cout << endl;
			for(int i=0; i<n; i++)
				cout << minimumas[i] << " ";
			cout << endl;*/
			cout << ats << "\n";
		}
		//cout << "kitas testas" << endl;
	}
	return 0;
}


/*input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			cout << a[0] << "\n";
			for(int i=0; i<q; i++)
			{
				int l, r;
				cin >> l >> r;
				cout << a[0] << "\n";
			}
			continue;
		}
		bool maksimumas[n], minimumas[n];

		for(int i=0; i<n; i++)
		{
			minimumas[i] = false;
			maksimumas[i] = false;
		}

		long long ats = 0;

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		auto prisiminti = [&](int num)
		{
			if(num==0)
			{
				if(a[0]>a[1])
				{
					ats += a[0];
					maksimumas[0] = true;
				}
			}
			else if(num==n-1)
			{
				if(a[n-1]>a[n-2])
				{
					ats += a[n-1];
					maksimumas[n-1] = true;
				}
			}
			else
			{
				if((a[num-1]<a[num]) && (a[num]>a[num+1]))
				{
					ats += a[num];
					maksimumas[num] = true;
				}
				if((a[num-1]>a[num]) && (a[num]<a[num+1]))
				{
					ats -= a[num];
					minimumas[num] = true;
				}
			}
		};

		for(int i=0; i<n; i++)
		{
			prisiminti(i);
		}

		cout << ats << "\n";

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
				pamirsti(i);

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
				pamirsti(i);

			while((l<0) || (r>=(int)a.size()))
			{
				id++;
			}
			swap(a[l], a[r]);

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}
			cout << ats << "\n";
		}
	}
	return 0;
}


/*input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			cout << a[0];
			for(int i=0; i<q; i++)
			{
				int l, r;
				cin >> l >> r;
				cout << a[0] << "\n";
			}
			continue;
		}
		bool maksimumas[n], minimumas[n];

		for(int i=0; i<n; i++)
		{
			minimumas[i] = false;
			maksimumas[i] = false;
		}

		long long ats = 0;

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		auto prisiminti = [&](int num)
		{
			if(num==0)
			{
				if(a[0]>a[1])
				{
					ats += a[0];
					maksimumas[0] = true;
				}
			}
			else if(num==n-1)
			{
				if(a[n-1]>a[n-2])
				{
					ats += a[n-1];
					maksimumas[n-1] = true;
				}
			}
			else
			{
				if((a[num-1]<a[num]) && (a[num]>a[num+1]))
				{
					ats += a[num];
					maksimumas[num] = true;
				}
				if((a[num-1]>a[num]) && (a[num]<a[num+1]))
				{
					ats -= a[num];
					minimumas[num] = true;
				}
			}
		};

		for(int i=0; i<n; i++)
		{
			prisiminti(i);
		}

		cout << ats << "\n";

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
				pamirsti(i);

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
				pamirsti(i);

			while((l<0) || (r>=(int)a.size()))
			{
				id++;
			}
			swap(a[l], a[r]);

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}
			cout << ats << "\n";
		}
	}
	return 0;
}


/*input
1
1 0
1
1 1
1
1 1
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			for(int i=0; i<=q; i++)
				cout << a[0] << "\n";
			continue;
		}
		bool maksimumas[n], minimumas[n];

		for(int i=0; i<n; i++)
		{
			minimumas[i] = false;
			maksimumas[i] = false;
		}

		long long ats = 0;

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		auto prisiminti = [&](int num)
		{
			if(num==0)
			{
				if(a[0]>a[1])
				{
					ats += a[0];
					maksimumas[0] = true;
				}
			}
			if(num==n-1)
			{
				if(a[n-1]>a[n-2])
				{
					ats += a[n-1];
					maksimumas[n-1] = true;
				}
			}
		};

		for(int i=0; i<n; i++)
		{
			prisiminti(i);
		}

		cout << ats << "\n";

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(1, l-5); i<=min(n-1, l+5); i++)
				pamirsti(i);

			for(int i=max(1, r-5); i<=min(n-1, r+5); i++)
				pamirsti(i);

			swap(a[l], a[r]);

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}
			cout << ats << "\n";
		}
	}
	return 0;
}


/*input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		int n, q;
		cin >> n >> q;
		//cout << "n ir q yra: " << n << " "<< q << endl;
		vector<long long> a(n);
		for(int i=0; i<n; i++)
			cin >> a[i];
		if(n==1)
		{
			for(int i=0; i<=q; i++)
				cout << a[0] << "\n";
			continue;
		}
		bool maksimumas[n], minimumas[n];

		for(int i=0; i<n; i++)
		{
			minimumas[i] = false;
			maksimumas[i] = false;
		}

		long long ats = 0;

		auto pamirsti = [&](int num)
		{
			if(maksimumas[num]==true)
			{
				ats -= a[num];
				maksimumas[num] = false;
			}
			if(minimumas[num]==true)
			{
				ats += a[num];
				minimumas[num] = false;
			}
		};

		auto prisiminti = [&](int num)
		{
			if(num==0)
			{
				if(a[0]>a[1])
				{
					ats += a[0];
					maksimumas[0] = true;
				}
			}
			else if(num==n-1)
			{
				if(a[n-1]>a[n-2])
				{
					ats += a[n-1];
					maksimumas[n-1] = true;
				}
			}
			else
			{
				if((a[num-1]<a[num]) && (a[num]>a[num+1]))
				{
					ats += a[num];
					maksimumas[num] = true;
				}
				if((a[num-1]>a[num]) && (a[num]<a[num+1]))
				{
					ats -= a[num];
					minimumas[num] = true;
				}
			}
		};

		for(int i=0; i<n; i++)
		{
			prisiminti(i);
		}

		cout << ats << "\n";

		for(int id=0; id<q; id++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
				pamirsti(i);

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
				pamirsti(i);

			swap(a[l], a[r]);

			for(int i=max(0, l-5); i<=min(n-1, l+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}

			for(int i=max(0, r-5); i<=min(n-1, r+5); i++)
			{
				pamirsti(i);
				prisiminti(i);
			}
			cout << ats << "\n";
		}
	}
	return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);
        vector < pair < int, int > > swapin(q);
        for(int& i : vec) cin>>i;
        for(auto& i : swapin) cin>>i.first>>i.second;

        int ans = vec[0];
        for(int i=1;i<n;i++){

            if(vec[i]>vec[i-1])
                ans += max(0LL, vec[i]-vec[i-1]);
        }
        cout<<ans<<endl;
        for(int i=0;i<q;i++){
            swap(vec[swapin[i].first-1] , vec[swapin[i].second-1]);
            ans = vec[0];
            for(int j=1;j<n;j++){

                if(vec[j]>vec[j-1])
                    ans += max(0LL, vec[j]-vec[j-1]);
                debug()<<imie(j) imie(vec[j]) imie(ans);
            }
            cout<<ans<<endl;
        }
    }

    return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);

        for(int& i : vec) cin>>i;

        int ans = vec[0];
        for(int i=1;i<n;i++){

            if(vec[i]>vec[i-1])
                ans += max(0LL, vec[i]-vec[i-1]);
        }

        cout<<ans<<endl;

        function < int (int) > MyFunc = [&](int x){
           if( x < 0 || x >= n)
               return 0LL;
           int curr = vec[x];
           int nxt = (x==n-1? 0:vec[x+1]);
           return max(0LL, curr-nxt);
        };
        for(int i=0;i<q;i++){
            int l,r;
            cin>>l>>r;
            l--, r--;
            ans -= MyFunc(l-1);
            ans -= MyFunc(l);

            if(l+1!=r)
                ans -= MyFunc(r-1);

            ans -= MyFunc(r);

            swap(vec[l], vec[r]);

            ans += MyFunc(l-1);
            ans += MyFunc(l);

            if(l+1!=r)
                ans += MyFunc(r-1);

            ans += MyFunc(r);

            cout<<ans<<endl;
        }

    }

    return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);

        for(int& i : vec) cin>>i;

        auto MyFunc = [&](int x){
            if(x < 0 || x >=n){
                return 0LL;
            }
            int cur = vec[x];
            int nxt = (x==n-1?0:vec[x+1]);
            return max(0LL, cur-nxt);
        };

        int ans =0;
        for(int i=0;i<n;i++){

                ans += MyFunc(vec[i]);
        }

        cout<<ans<<endl;

        for(int i=0;i<q;i++){
            int l,r;
            cin>>l>>r;
            l--, r--;

            ans -= MyFunc(l-1);
            ans -= MyFunc(l);

            if(l+1!=r)
                ans -= MyFunc(r-1);

            ans -= MyFunc(r);

            swap(vec[l], vec[r]);

            ans += MyFunc(l-1);
            ans += MyFunc(l);

            if(l+1!=r)
                ans += MyFunc(r-1);

            ans += MyFunc(r);

            cout<<ans<<endl;
        }

    }

    return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);

        for(int& i : vec) cin>>i;

        int ans = vec[0];
        for(int i=1;i<n;i++){

            if(vec[i]>vec[i-1])
                ans += max(0LL, vec[i]-vec[i-1]);
        }

        cout<<ans<<endl;

        function < int (int) > MyFunc = [&](int x){
           if( x < 0 || x >= n)
               return 0LL;
           int curr = vec[x];
           int nxt = (x==n-1? 0:vec[x+1]);
           return max(0LL, curr-nxt);
        };
        for(int i=0;i<q;i++){
            int l,r;
            cin>>l>>r;
            l--, r--;
            ans -= MyFunc(l-1);
            ans -= MyFunc(l);

            if(l+1!=r)
                ans -= MyFunc(r-1);

            ans -= MyFunc(r);

            swap(vec[l], vec[r]);

            ans += MyFunc(l-1);
            ans += MyFunc(l);

            if(l+1!=r)
                ans += MyFunc(r-1);

            ans += MyFunc(r);

            cout<<ans<<endl;
        }

    }

    return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);

        for(int& i : vec) cin>>i;

        int ans = vec[0];
        for(int i=1;i<n;i++){

            if(vec[i]>vec[i-1])
                ans += max(0LL, vec[i]-vec[i-1]);
        }

        cout<<ans<<endl;

        auto MyFunc = [&](int x){
            if(x < 0 || x >=n){
                return 0LL;
            }
            int cur = vec[x];
            int nxt = (x==n-1?0:vec[x+1]);
            return max(0LL, cur-nxt);
        };

        for(int i=0;i<q;i++){
            int l,r;
            cin>>l>>r;
            l--, r--;

            ans -= MyFunc(l-1);
            ans -= MyFunc(l);

            if(l+1!=r)
                ans -= MyFunc(r-1);

            ans -= MyFunc(r);

            swap(vec[l], vec[r]);

            ans += MyFunc(l-1);
            ans += MyFunc(l);

            if(l+1!=r)
                ans += MyFunc(r-1);

            ans += MyFunc(r);

            cout<<ans<<endl;
        }

    }

    return 0;
}


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

#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define moduloP 1e9 + 7

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

signed main(){
    int test;
    cin>>test;
    while(test--){
        int n,q;
        cin>>n>>q;

        vector < int > vec(n);

        for(int& i : vec) cin>>i;

        int ans = vec[0];
        for(int i=1;i<n;i++){

            if(vec[i]>vec[i-1])
                ans += max(0LL, vec[i]-vec[i-1]);
        }

        cout<<ans<<endl;

        function < int (int) > MyFunc = [&](int x){
           if( x < 0 || x >= n)
               return 0LL;
           int curr = vec[x];
           int nxt = vec[x+1];
           (x==n-1?nxt = 0:nxt=vec[x+1]);
           return max(0LL, curr-nxt);
        };
        for(int i=0;i<q;i++){
            int l,r;
            cin>>l>>r;
            l--, r--;
            ans -= MyFunc(l-1);
            ans -= MyFunc(l);

            if(l+1!=r)
                ans -= MyFunc(r-1);

            ans -= MyFunc(r);

            swap(vec[l], vec[r]);

            ans += MyFunc(l-1);
            ans += MyFunc(l);

            if(l+1!=r)
                ans += MyFunc(r-1);

            ans += MyFunc(r);

            cout<<ans<<endl;
        }

    }

    return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n,q;
	    cin>>n>>q;
	    vector<int> a(n);
	    for(int i=0;i<n;i++){
	        cin>>a[i];
	    }
	    long long int ans=a[0];
	    for(int i=1;i<n;i++){
	        ans+=max(0, a[i]-a[i-1]);
	    }
	    cout<<ans<<"\n";
	    a.push_back(0);
	    a.insert(a.begin(), 0);
	    for(int i=0;i<q;i++){
	        int l,r;
	        cin>>l>>r;
	        ans=ans-max(0, a[l]-a[l-1]);
	        ans=ans-max(0, a[l+1]-a[l]);
	        ans=ans-max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans-max(0, a[r]-a[r-1]);

	        swap(a[l], a[r]);

	        ans=ans+max(0, a[l]-a[l-1]);
	        ans=ans+max(0, a[l+1]-a[l]);
	        ans=ans+max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans+max(0, a[r]-a[r-1]);
	        cout<<ans<<"\n";

	    }
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	int a[300100];
	while(t--){
	    int n,q;
	    cin>>n>>q;
	    for(int i=1;i<=n;i++){
	        cin>>a[i];
	    }
	    a[n+1]=0;
	    long long int ans=0;
	    for(int i=1;i<=n;i++){
	        ans+=max(0, a[i]-a[i-1]);
	    }
	    cout<<ans<<"\n";
	    for(int i=0;i<q;i++){
	        int l,r;
	        cin>>l>>r;
	        ans=ans-max(0, a[l]-a[l-1]);
	        ans=ans-max(0, a[l+1]-a[l]);
	        ans=ans-max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans-max(0, a[r]-a[r-1]);

	        swap(a[l], a[r]);

	        ans=ans+max(0, a[l]-a[l-1]);
	        ans=ans+max(0, a[l+1]-a[l]);
	        ans=ans+max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans+max(0, a[r]-a[r-1]);
	        cout<<ans<<"\n";

	    }
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n,q;
	    cin>>n>>q;
	    vector<int> a(n+5,0);
	    for(int i=1;i<=n;i++){
	        cin>>a[i];
	    }
	    long long int ans=0;
	    for(int i=1;i<=n;i++){
	        ans+=max(0, a[i]-a[i-1]);
	    }
	    cout<<ans<<"\n";
	    for(int i=0;i<q;i++){
	        int l,r;
	        cin>>l>>r;
	        ans=ans-max(0, a[l]-a[l-1]);
	        ans=ans-max(0, a[l+1]-a[l]);
	        ans=ans-max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans-max(0, a[r]-a[r-1]);

	        swap(a[l], a[r]);

	        ans=ans+max(0, a[l]-a[l-1]);
	        ans=ans+max(0, a[l+1]-a[l]);
	        ans=ans+max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans+max(0, a[r]-a[r-1]);
	        cout<<ans<<"\n";

	    }
	}
	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n,q;
	    cin>>n>>q;
	    vector<int> a(n);
	    for(int i=0;i<n;i++){
	        cin>>a[i];
	    }
	    int ans=a[0];
	    for(int i=1;i<n;i++){
	        ans+=max(0, a[i]-a[i-1]);
	    }
	    cout<<ans<<"\n";
	    a.push_back(0);
	    a.insert(a.begin(), 0);
	    for(int i=0;i<q;i++){
	        int l,r;
	        cin>>l>>r;
	        ans=ans-max(0, a[l]-a[l-1]);
	        ans=ans-max(0, a[l+1]-a[l]);
	        ans=ans-max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans-max(0, a[r]-a[r-1]);

	        swap(a[l], a[r]);

	        ans=ans+max(0, a[l]-a[l-1]);
	        ans=ans+max(0, a[l+1]-a[l]);
	        ans=ans+max(0, a[r+1]-a[r]);
	        if((r-1)!=l)ans=ans+max(0, a[r]-a[r-1]);
	        cout<<ans<<"\n";

	    }
	}
	return 0;
}


std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int a, int b) {
    return uniform_int_distribution<int>(a, b)(rng);
}

ll power(ll x, ll y, ll p){
   ll res = 1;
   x = x % p;
   while (y > 0) {
      if (y & 1)
         res = (res * x) % p;
      y = y >> 1;
      x = (x * x) % p;
   }
   return res;
}

const ll INF = (ll)(8e18);
const ll MOD = (ll)(1e9 + 7);
const ll HMOD = (ll)4222234727;
const ll Hashprime = (ll)31;

const int maxn = 300100;

ll a[maxn];

int32_t main(){
    IOS;
    #ifndef ONLINE_JUDGE
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt", "w", stdout);
    #endif
    int TestCases; cin >> TestCases;
    while(TestCases--){
        ll n,q; cin >> n >> q;
        ll ans = 0;
        for(ll i = 1; i <= n; i++) cin >> a[i];
        for(ll i = 1; i <= n; i++){
           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 << '\n';
        ll how = 2;
        while(q--){
          ll l,r; cin >> l >> r;
          map <ll, ll> mp;
          for(ll i = max(1LL, l - how); i <= min(n, l + how); i++){
            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];
            mp[i] += 1;
          }
          for(ll i = max(1LL, r - how); i <= min(n, r + how); i++){
            if(mp.find(i) != mp.end()) continue;
            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];
          }
          swap(a[l], a[r]);
          mp.clear();
          for(ll i = max(1LL, l - how); i <= min(n, l + how); i++){
            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];
            mp[i] += 1;
          }
          for(ll i = max(1LL, r - how); i <= min(n, r + how); i++){
            if(mp.find(i) != mp.end()) continue;
            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 << '\n';
        }
        for(ll i = 1; i <= n; i++) a[i] = 0;
    }
    #ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int a, int b) {
    return uniform_int_distribution<int>(a, b)(rng);
}

ll power(ll x, ll y, ll p){
   ll res = 1;
   x = x % p;
   while (y > 0) {
      if (y & 1)
         res = (res * x) % p;
      y = y >> 1;
      x = (x * x) % p;
   }
   return res;
}

const ll INF = (ll)(8e18);
const ll MOD = (ll)(1e9 + 7);
const ll HMOD = (ll)4222234727;
const ll Hashprime = (ll)31;

const int maxn = 300100;

ll a[maxn];

int32_t main(){
    IOS;
    #ifndef ONLINE_JUDGE
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt", "w", stdout);
    #endif
    int TestCases; cin >> TestCases;
    while(TestCases--){
        ll n,q; cin >> n >> q;
        ll ans = 0;
        for(ll i = 1; i <= n; i++) cin >> a[i];
        for(ll i = 1; i <= n; i++){
           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 << '\n';
        ll how = 10;
        while(q--){
          ll l,r; cin >> l >> r;
          map <ll, ll> mp;
          for(ll i = max(1LL, l - how); i <= min(n, l + how); i++){
            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];
            mp[i] += 1;
          }
          for(ll i = max(1LL, r - how); i <= min(n, r + how); i++){
            if(mp.find(i) != mp.end()) continue;
            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];
          }
          swap(a[l], a[r]);
          mp.clear();
          for(ll i = max(1LL, l - how); i <= min(n, l + how); i++){
            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];
            mp[i] += 1;
          }
          for(ll i = max(1LL, r - how); i <= min(n, r + how); i++){
            if(mp.find(i) != mp.end()) continue;
            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 << '\n';
        }
        for(ll i = 1; i <= n; i++) a[i] = 0;
    }
    #ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#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';
        }
    }
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

#define int long long
const int N = 3e5 + 1;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}


#pragma Ofast
#pragma loop-opt(on)
#include<iostream>
#include<algorithm>
using namespace std;

#define int long long
const int N = 3e5 + 10;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

#define int long long
const int N = 3e5 + 1;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);#pragma Ofast#pragma loop-opt(on


#pragma Ofast
#pragma loop-opt(on)
#include<iostream>
#include<algorithm>
using namespace std;

// #define int long long
const int N = 3e5 + 1;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}


#pragma Ofast
#pragma loop-opt(on)
#include<iostream>
#include<algorithm>
using namespace std;

// #define int long long
const int N = 3e5 + 10;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}


#pragma Ofast
#pragma loop-opt(on)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

#define int long long
const int N = 3e5 + 1;
int t, n, q, a[N], ans;
inline void erase(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans -= a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans += a[i];
}
inline void insert(int i){
	if(i == 0 or i == n + 1) return;
	if(a[i-1] < a[i] and a[i+1] < a[i]) ans += a[i];
	if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> t;
	for(; t; --t){
		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] and a[i+1] < a[i]) ans += a[i];
			if(a[i-1] > a[i] and a[i+1] > a[i]) ans -= a[i];
		}
		cout << ans << '\n';
		for(int l, r; q; --q){
			cin >> l >> r;
			erase(l-1), erase(l), erase(l+1);
			if(l != r){
				if(r - 1 != l and r - 1 != l + 1) 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 and r - 1 != l + 1) insert(r - 1);
				if(r != l + 1) insert(r);
				insert(r + 1);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (ll i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << "\n";
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << "\n";
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (ll i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

ll arr[1000000+5], ans=0, n;
void insert(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        ll q;
        cin >> n >> q;
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(L-1);
            remove(L);
            remove(L+1);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(R-1);
                if (R != L+1) remove(R);
                remove(R+1);
            }
            swap(arr[L], arr[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 << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    int t;
    cin >> t;
    while (t-- > 0)
    {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (ll i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    int t;
    cin >> t;
    while (t-- > 0)
    {
        int n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

ll arr[1000000+5], ans=0, n;
void insert(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        ll q;
        cin >> n >> q;
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(L-1);
            remove(L);
            remove(L+1);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(R-1);
                if (R != L+1) remove(R);
                remove(R+1);
            }
            swap(arr[L], arr[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 << endl;
        }
    }
    return 0;
}