#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<bits/stdc++.h>                     //....hello world
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ll long long int
// #define SORTA(v) sort(v.begin(),v.end())
// #define SORTD(v) sort(v.begin(),v.end(),greater<>())
#define endl '\n'
// #define nod(N) floor(log10(N)) + 1
// #define LB(a) lower_bound(a)
// #define UB(a) upper_bound(a)
// #define F first
// #define S second
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
// #define REVERSE(s) reverse(s.begin(),s.end())
// #define LBV(v,a) lower_bound(v.begin(),v.end(),a)
// #define UBV(v,a) upper_bound(v.begin(),v.end(),a)
// #define GCD(a,b) __gcd(a,b)
// #define ITX(it,x1) for(auto it = x1.begin();it!=x1.end();it++)
// #define I insert
// #define VFIND(v1,val) find(v1.begin() , v1.end() , val )
// #define VFILL(v1 , val)   std::fill(v1.begin(), v1.end(), val);
// #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
// #define all(x) x.begin() , x.end()

// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// typedef tree<int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// ll mxm() {return LLONG_MIN;}
// template<typename... Args>
// ll mxm(ll a, Args... args) { return max(a,mxm(args...)); }
// ll mnm() {return LLONG_MAX;}
// template<typename... Args>
// ll mnm(ll a, Args... args) { return min(a,mnm(args...)); }

// const int MOD = 1e9 + 7;
// long long int powxy(long long int x, long long int y)
// {
//     if (y == 0) return 1;if (y%2 == 1) return (x*powxy(x, y-1))%MOD;
//     long long int t = powxy(x, y/2);return (t*t)%MOD;
// }
// void DPM(map<ll,ll>m1)
// {
//     for(auto it = m1.begin();it!=m1.end();it++)
//     {
//         cout<<it->first<<" "<<it->second<<endl;
//     }
//     cout<<endl;
// }
// void DPV(vector<ll>v)
// {
//     FOR(i,v.size())cout<<v[i]<<" ";cout<<endl;
// }
// void DPS(set<ll>s1)
// {
//     for(auto it =s1.begin();it!=s1.end();it++)cout<<*it<<" ";cout<<endl;
// }
// void DPA(ll arr[],ll n){FOR(i,n)cout<<arr[i]<<" ";cout<<endl;}

vector<ll>v1(1000000);

ll ans = 0 , n ;

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

void insert(ll i)
{
    if(i > n || i == 0)return;

    if( v1[i] > v1[i-1] && v1[i] > v1[i+1])ans += v1[i];
    if( v1[i] < v1[i-1] && v1[i] < v1[i+1])ans -= v1[i];
}

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll q; cin>>n>>q;

        FOR1(i,n)cin>>v1[i];
        i=0; ans = 0 ;

        v1[0] = -99999999999;
        v1[n+1] = -9999999999;

        i=1;
        while(i<=n)
        {
            while(i+1<=n && v1[i] < v1[i+1])i++;

            ans += v1[i]; i++;

            while( i+1<=n && v1[i] > v1[i+1] )i++;
            if(i >= n-1 )break;
            else ans -= v1[i];
            i++;
        }
        cout<<ans<<endl;

        while(q--)
        {
            ll L,R; cin>>L>>R;

            if(L == R)
            {
                cout<<ans<<endl; continue;
            }

            erase(L); erase(L-1); erase(L+1);
            if(R-1 != L-1 && R-1 != L+1 && R-1 != L)erase(R-1) ;
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)erase(R+1) ;
            swap(v1[L] , v1[R]);
            insert(L); insert(L+1); insert(L-1);

            if(R-1 != L-1 && R-1 != L+1 && R-1 != L )insert(R-1);
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)insert(R+1);

            cout<<ans<<endl;

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define endl '\n'
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);

vector<ll>v1(1000000);

ll ans = 0 , n ;

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

void insert(ll i)
{
    if(i > n || i == 0)return;

    if( v1[i] > v1[i-1] && v1[i] > v1[i+1])ans += v1[i];
    if( v1[i] < v1[i-1] && v1[i] < v1[i+1])ans -= v1[i];
}

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll q; cin>>n>>q;

        FOR1(i,n)cin>>v1[i];
        i=0; ans = 0 ;

        v1[0] =  -9999999999999;
        v1[n+1]= -9999999999999;

        i=1;
        while(i<=n)
        {
            while(i+1<=n && v1[i] < v1[i+1])i++;

            ans += v1[i]; i++;

            while( i+1<=n && v1[i] > v1[i+1] )i++;
            if(i >= n-1 )break;
            else ans -= v1[i];
            i++;
        }
        cout<<ans<<endl;

        while(q--)
        {
            ll L,R; cin>>L>>R;

            if(L == R)
            {
                cout<<ans<<endl; continue;
            }

            erase(L); erase(L-1); erase(L+1);
            if(R-1 != L-1 && R-1 != L+1 && R-1 != L)erase(R-1) ;
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)erase(R+1) ;
            if(R != L-1 && R!= L+1 )erase(R);

            swap(v1[L] , v1[R]);

            insert(L); insert(L+1); insert(L-1);

            if(R-1 != L-1 && R-1 != L+1 && R-1 != L )insert(R-1);
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)insert(R+1);
            if(R != L-1 && R!= L+1 )insert(R);

            cout<<ans<<endl;

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define endl '\n'
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);

vector<ll>v1(1000000);

ll ans = 0 , n ;

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

void insert(ll i)
{
    if(i > n || i == 0)return;

    if( v1[i] > v1[i-1] && v1[i] > v1[i+1])ans += v1[i];
    if( v1[i] < v1[i-1] && v1[i] < v1[i+1])ans -= v1[i];
}

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll q; cin>>n>>q;

        FOR1(i,n)cin>>v1[i];
        i=0; ans = 0 ;

        v1[0]   = -1;
        v1[n+1] = -1;

        i=1;

        while(i<=n)
        {
            while(i+1<=n && v1[i] < v1[i+1])i++;

            ans += v1[i]; i++;

            while( i+1<=n && v1[i] > v1[i+1] )i++;
            if(i >= n )break;
            else ans -= v1[i];

            i++;

        }

        cout<<ans<<endl;

        while(q--)
        {
            ll L,R; cin>>L>>R;

            if(L == R)
            {
                cout<<ans<<endl; continue;
            }

            erase(L); erase(L-1); erase(L+1);
            if( R-1 != L+1 && R-1 != L)erase(R-1) ;
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)erase(R+1) ;
            if( R!= L+1 && R != L-1 )erase(R);

            swap(v1[L] , v1[R]);

            insert(L); insert(L+1); insert(L-1);

            if(R-1 != L-1 && R-1 != L+1 && R-1 != L )insert(R-1);
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)insert(R+1);
            if(R != L-1 && R!= L+1 )insert(R);

            cout<<ans<<endl;
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define endl '\n'
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);

vector<ll>v1(1000000);

ll ans = 0 , n ;

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

void insert(ll i)
{
    if(i > n || i == 0)return;

    if( v1[i] > v1[i-1] && v1[i] > v1[i+1])ans += v1[i];
    if( v1[i] < v1[i-1] && v1[i] < v1[i+1])ans -= v1[i];
}

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll q; cin>>n>>q;

        FOR1(i,n)cin>>v1[i];
        i=0; ans = 0 ;

        v1[0]   = -1;
        v1[n+1] = -1;

        i=1;

        while(i<=n)
        {
            insert(i); i++;
        }
        cout<<ans<<endl;

        while(q--)
        {
            ll L,R; cin>>L>>R;

            if(L == R)
            {
                cout<<ans<<endl; continue;
            }

            erase(L); erase(L-1); erase(L+1);
            if( R-1 != L+1 && R-1 != L)erase(R-1) ;
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)erase(R+1) ;
            if( R!= L+1 && R != L-1 )erase(R);

            swap(v1[L] , v1[R]);

            insert(L); insert(L+1); insert(L-1);

            if(R-1 != L-1 && R-1 != L+1 && R-1 != L )insert(R-1);
            if(R+1 != L-1 && R+1 != L+1 && R+1 != L)insert(R+1);
            if(R != L-1 && R!= L+1 )insert(R);

            cout<<ans<<endl;
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define endl '\n'
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);

vector<ll>v1(1000000);

ll ans = 0 , n ;

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

void insert(ll i)
{
    if(i > n || i == 0)return;

    if( v1[i] > v1[i-1] && v1[i] > v1[i+1])ans += v1[i];
    if( v1[i] < v1[i-1] && v1[i] < v1[i+1])ans -= v1[i];
}

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll q; cin>>n>>q;

        FOR1(i,n)cin>>v1[i];
        i=0; ans = 0 ;

        v1[0]   = -1;
        v1[n+1] = -1;

        i=1;

        while(i<=n)
        {
            while(i+1<=n && v1[i] < v1[i+1])i++;

            ans += v1[i]; i++;

            while( i+1<=n && v1[i] > v1[i+1] )i++;
            if(i >= n-1 )break;
            else ans -= v1[i];
            i++;
        }
        cout<<ans<<endl;

        while(q--)
        {
            // ll L,R; cin>>L>>R;

            // if(L == R)
            // {
            //     cout<<ans<<endl; continue;
            // }

            // erase(L); erase(L-1); erase(L+1);
            // if( R-1 != L+1 && R-1 != L)erase(R-1) ;
            // if(R+1 != L-1 && R+1 != L+1 && R+1 != L)erase(R+1) ;
            // if( R!= L+1 )erase(R);

            // swap(v1[L] , v1[R]);

            // insert(L); insert(L+1); insert(L-1);

            // if(R-1 != L-1 && R-1 != L+1 && R-1 != L )insert(R-1);
            // if(R+1 != L-1 && R+1 != L+1 && R+1 != L)insert(R+1);
            // if(R != L-1 && R!= L+1 )insert(R);

            // cout<<ans<<endl;

            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(v1[l],v1[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;

        }

    }
}


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

const int MAX = (1 << 19), MN = 300003;
LL mxP[2*MAX], mxN[2*MAX], mnP[2*MAX], mnN[2*MAX];
int mxLP[2*MAX], mxLN[2*MAX], mnLP[2*MAX], mnLN[2*MAX];
int inp[MN];

void Update(int a, int i){
	i += MAX;
	mxN[i] = a;
	mnN[i] = a;
	mxP[i] = 0;
	mnP[i] = 0;
	mxLN[i] = 1;
	mnLN[i] = 1;
	mxLP[i] = 0;
	mnLP[i] = 0;
	i >>= 1;
	while(i > 0){

		mxP[i] = mxP[i*2] + mxP[i*2+1];
		mxLP[i] = mxLP[i*2] + mxLP[i*2+1];

		if(mxP[i] < mxN[i*2] - mnN[i*2+1]){
			mxP[i] = mxN[i*2] - mnN[i*2+1];
			mxLP[i] = mxLN[i*2] + mnLN[i*2+1];
		}

		mxN[i] = mxP[i*2] + mxN[i*2+1];
		mxLN[i] = mxLP[i*2] + mxLN[i*2+1];

		if(mxN[i] < mxN[i*2] - mnP[i*2+1]){
			mxN[i] = mxN[i*2] - mnP[i*2+1];
			mxLN[i] = mxLN[i*2] + mnLP[i*2+1];
		}

		mnP[i] = mnP[i*2] + mnP[i*2+1];
		mnLP[i] = mnLP[i*2] + mnLP[i*2+1];

		if(mnP[i] > mnN[i*2] - mxN[i*2+1]){
			mnP[i] = mnN[i*2] - mxN[i*2+1];
			mnLP[i] = mnLN[i*2] + mxLN[i*2+1];
		}

		mnN[i] = mnP[i*2] + mnN[i*2+1];
		mnLN[i] = mnLP[i*2] + mnLN[i*2+1];

		if(mnN[i] > mnN[i*2] - mxP[i*2+1]){
			mnN[i] = mnN[i*2] - mxP[i*2+1];
			mnLN[i] = mnN[i*2] + mxLP[i*2+1];
		}

		i >>= 1;
	}
}

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		for(int i = 0; i < n; i++){
			cin >> inp[i];
			Update(inp[i], i);
		}

		cout << max(mxN[1], mxP[1]) << "\n";

		for(int i = 0; i < q; i++){
			int a, b; cin >> a >> b;
			Update(inp[b-1], a-1);
			Update(inp[a-1], b-1);
			swap(inp[a-1], inp[b-1]);
			cout << max(mxN[1], mxP[1]) << "\n";
		}

		for(int i = 0; i < n; i++) Update(0, i);
	}
	return 0;
}


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

const int MAX = (1 << 19), MN = 300003;
LL mxP[2*MAX], mxN[2*MAX], mnP[2*MAX], mnN[2*MAX];
int mxLP[2*MAX], mxLN[2*MAX], mnLP[2*MAX], mnLN[2*MAX];
int inp[MN], MX;

void Update(int a, int i){
	i += MX;
	mxN[i] = a;
	mnN[i] = a;
	mxP[i] = 0;
	mnP[i] = 0;
	mxLN[i] = 1;
	mnLN[i] = 1;
	mxLP[i] = 0;
	mnLP[i] = 0;
	i >>= 1;
	while(i > 0){

		if(mxP[i*2] + mxP[i*2+1] < mxN[i*2] - mnN[i*2+1]){
			mxP[i] = mxN[i*2] - mnN[i*2+1];
			mxLP[i] = mxLN[i*2] + mnLN[i*2+1];
		}
		else{
			mxP[i] = mxP[i*2] + mxP[i*2+1];
			mxLP[i] = mxLP[i*2] + mxLP[i*2+1];
		}

		if(mxP[i*2] + mxN[i*2+1] < mxN[i*2] - mnP[i*2+1]){
			mxN[i] = mxN[i*2] - mnP[i*2+1];
			mxLN[i] = mxLN[i*2] + mnLP[i*2+1];
		}
		else{
			mxN[i] = mxP[i*2] + mxN[i*2+1];
			mxLN[i] = mxLP[i*2] + mxLN[i*2+1];
		}

		if(mnP[i*2] + mnP[i*2+1] > mnN[i*2] - mxN[i*2+1]){
			mnP[i] = mnN[i*2] - mxN[i*2+1];
			mnLP[i] = mnLN[i*2] + mxLN[i*2+1];
		}
		else{
			mnP[i] = mnP[i*2] + mnP[i*2+1];
			mnLP[i] = mnLP[i*2] + mnLP[i*2+1];
		}

		if(mnP[i*2] + mnN[i*2+1] > mnN[i*2] - mxP[i*2+1]){
			mnN[i] = mnN[i*2] - mxP[i*2+1];
			mnLN[i] = mnN[i*2] + mxLP[i*2+1];
		}
		else{
			mnN[i] = mnP[i*2] + mnN[i*2+1];
			mnLN[i] = mnLP[i*2] + mnLN[i*2+1];
		}

		i >>= 1;
	}
}

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

		int tempn = n; MX = 1;
		while(tempn > 0) {tempn >>= 1; MX <<= 1;}

		for(int i = 0; i < n; i++){
			cin >> inp[i];
			Update(inp[i], i);
		}

		cout << max(mxN[1], mxP[1]) << "\n";

		for(int i = 0; i < q; i++){
			int a, b; cin >> a >> b;
			Update(inp[b-1], a-1);
			Update(inp[a-1], b-1);
			swap(inp[a-1], inp[b-1]);
			cout << max(mxN[1], mxP[1]) << "\n";
		}

		for(int i = 0; i < n; i++) Update(0, i);
	}
	return 0;
}


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

const int MAX = (1 << 19), MN = 300003;
LL mxP[2*MAX], mxN[2*MAX], mnP[2*MAX], mnN[2*MAX];
int mxLP[2*MAX], mxLN[2*MAX], mnLP[2*MAX], mnLN[2*MAX];
int inp[MN];

void Update(int a, int i){
	i += MAX;
	mxN[i] = a;
	mnN[i] = a;
	mxP[i] = 0;
	mnP[i] = 0;
	mxLN[i] = 1;
	mnLN[i] = 1;
	mxLP[i] = 0;
	mnLP[i] = 0;
	i >>= 1;
	while(i > 0){

		if(mxP[i*2] + mxP[i*2+1] < mxN[i*2] - mnN[i*2+1]){
			mxP[i] = mxN[i*2] - mnN[i*2+1];
			mxLP[i] = mxLN[i*2] + mnLN[i*2+1];
		}
		else{
			mxP[i] = mxP[i*2] + mxP[i*2+1];
			mxLP[i] = mxLP[i*2] + mxLP[i*2+1];
		}

		if(mxP[i*2] + mxN[i*2+1] < mxN[i*2] - mnP[i*2+1]){
			mxN[i] = mxN[i*2] - mnP[i*2+1];
			mxLN[i] = mxLN[i*2] + mnLP[i*2+1];
		}
		else{
			mxN[i] = mxP[i*2] + mxN[i*2+1];
			mxLN[i] = mxLP[i*2] + mxLN[i*2+1];
		}

		if(mnP[i*2] + mnP[i*2+1] > mnN[i*2] - mxN[i*2+1]){
			mnP[i] = mnN[i*2] - mxN[i*2+1];
			mnLP[i] = mnLN[i*2] + mxLN[i*2+1];
		}
		else{
			mnP[i] = mnP[i*2] + mnP[i*2+1];
			mnLP[i] = mnLP[i*2] + mnLP[i*2+1];
		}

		if(mnP[i*2] + mnN[i*2+1] > mnN[i*2] - mxP[i*2+1]){
			mnN[i] = mnN[i*2] - mxP[i*2+1];
			mnLN[i] = mnN[i*2] + mxLP[i*2+1];
		}
		else{
			mnN[i] = mnP[i*2] + mnN[i*2+1];
			mnLN[i] = mnLP[i*2] + mnLN[i*2+1];
		}

		i >>= 1;
	}
}

int main(){
	int t; cin >> t;
	int lastn = 0;
	while(t--){
		int n, q; cin >> n >> q;

		if(lastn > n)
			for(int i = n; i < lastn; i++)
				Update(0, i);

		lastn = n;

		for(int i = 0; i < n; i++){
			cin >> inp[i];
			Update(inp[i], i);
		}

		cout << max(mxN[1], mxP[1]) << "\n";

		for(int i = 0; i < q; i++){
			int a, b; cin >> a >> b;
			Update(inp[b-1], a-1);
			Update(inp[a-1], b-1);
			swap(inp[a-1], inp[b-1]);
			cout << max(mxN[1], mxP[1]) << "\n";
		}

		for(int i = 0; i < n; i++) Update(0, i);
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;

const int MN = 300003;
int inp[MN];

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		LL even = 0, odd = 0;
		LL tEven = 0, tOdd = 0;
		for(int i = 1; i <= n; i++){
			cin >> inp[i];
			tOdd = max(odd, even + inp[i]);
			tEven = max(even, odd - inp[i]);
			odd = tOdd;
			even = tEven;
		}

		LL result = max(odd, even);
		cout << result << "\n";

		while(q--){
			int x, y; cin >> x >> y;

			if(x > y) swap(x, y);

			if(y - x == 1){
				int a = inp[x-1], b = inp[x], c = inp[y], d = inp[y+1];

				if	   (a < b && a > c) result += a;
				else if(a > b && a < c) result -= a;

				if	   (b < a && b > d) result += b;
				else if(b > a && b < d) result -= b;

				if	   (c > a && c < d) result += c;
				else if(c < a && c > d) result -= c;

				if		(d < c && d > b) result += d;
				else if (d > c && d < b) result -= d;
			}
			else{
				int a = inp[x-1], b = inp[x], c = inp[x+1], b1 = inp[y];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;

				a = inp[y-1]; b = inp[y]; c = inp[y+1]; b1 = inp[x];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;
			}

			swap(inp[x], inp[y]);
			cout << result << "\n";
		}
		for(int i = 0; i <= n; i++) inp[i] = 0;
	}
	return 0;
}


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

const int MAX = (1 << 19), MN = 300003;
LL mxP[2*MAX], mxN[2*MAX], mxLP[2*MAX], mxLN[2*MAX], mnP[2*MAX], mnN[2*MAX], mnLP[2*MAX], mnLN[2*MAX];
int inp[MN];

void Update(LL a, int i){
	i += MAX;
	mxN[i] = a;
	mnN[i] = a;
	mxP[i] = 0;
	mnP[i] = 0;
	mxLN[i] = 1;
	mnLN[i] = 1;
	mxLP[i] = 0;
	mnLP[i] = 0;
	i >>= 1;
	while(i > 0){

		mxP[i] = mxP[i*2] + mxP[i*2+1];
		mxLP[i] = mxLP[i*2] + mxLP[i*2+1];

		if(mxP[i] < mxN[i*2] - mnN[i*2+1]){
			mxP[i] = mxN[i*2] - mnN[i*2+1];
			mxLP[i] = mxLN[i*2] + mnLN[i*2+1];
		}

		mxN[i] = mxP[i*2] + mxN[i*2+1];
		mxLN[i] = mxLP[i*2] + mxLN[i*2+1];

		if(mxN[i] < mxN[i*2] - mnP[i*2+1]){
			mxN[i] = mxN[i*2] - mnP[i*2+1];
			mxLN[i] = mxLN[i*2] + mnLP[i*2+1];
		}

		mnP[i] = mnP[i*2] + mnP[i*2+1];
		mnLP[i] = mnLP[i*2] + mnLP[i*2+1];

		if(mnP[i] > mnN[i*2] - mxN[i*2+1]){
			mnP[i] = mnN[i*2] - mxN[i*2+1];
			mnLP[i] = mnLN[i*2] + mxLN[i*2+1];
		}

		mnN[i] = mnP[i*2] + mnN[i*2+1];
		mnLN[i] = mnLP[i*2] + mnLN[i*2+1];

		if(mnN[i] > mnN[i*2] - mxP[i*2+1]){
			mnN[i] = mnN[i*2] - mxP[i*2+1];
			mnLN[i] = mnN[i*2] + mxLP[i*2+1];
		}

		i >>= 1;
	}
}

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		for(int i = 0; i < n; i++){
			cin >> inp[i];
			Update(inp[i], i);
		}

		cout << max(mxN[1], mxP[1]) << "\n";

		for(int i = 0; i < q; i++){
			int a, b; cin >> a >> b;
			Update(inp[b-1], a-1);
			Update(inp[a-1], b-1);
			swap(inp[a-1], inp[b-1]);
			cout << max(mxN[1], mxP[1]) << "\n";
		}

		for(int i = 0; i < n; i++) Update(0, i);
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;

const int MN = 300003;
int inp[MN];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		LL even = 0, odd = 0;
		LL tEven = 0, tOdd = 0;
		inp[0] = 0; inp[n+1] = 0;
		for(int i = 1; i <= n; i++){
			cin >> inp[i];
			tOdd = max(odd, even + inp[i]);
			tEven = max(even, odd - inp[i]);
			odd = tOdd;
			even = tEven;
		}

		LL result = max(odd, even);
		cout << result << "\n";

		while(q--){
			int x, y; cin >> x >> y;

			if(x > y) swap(x, y);

			if(y - x == 1){
				int a = inp[x-1], b = inp[x], c = inp[y], d = inp[y+1];

				if	   (a < b && a > c) result += a;
				else if(a > b && a < c) result -= a;

				if	   (b < a && b > d) result += b;
				else if(b > a && b < d) result -= b;

				if	   (c > a && c < d) result += c;
				else if(c < a && c > d) result -= c;

				if		(d < c && d > b) result += d;
				else if (d > c && d < b) result -= d;
			}
			else{
				int a = inp[x-1], b = inp[x], c = inp[x+1], b1 = inp[y];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;

				a = inp[y-1]; b = inp[y]; c = inp[y+1]; b1 = inp[x];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;
			}

			swap(inp[x], inp[y]);
			cout << result << "\n";
		}
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;

const int MN = 300003;
LL inp[MN];

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		LL even = 0, odd = 0;
		LL tEven = 0, tOdd = 0;
		for(int i = 1; i <= n; i++){
			cin >> inp[i];
			tOdd = max(odd, even + inp[i]);
			tEven = max(even, odd - inp[i]);
			odd = tOdd;
			even = tEven;
		}

		LL result = max(odd, even);
		cout << result << "\n";

		while(q--){
			int x, y; cin >> x >> y;

			if(x > y) swap(x, y);

			if(y - x == 1){
				LL a = inp[x-1], b = inp[x], c = inp[y], d = inp[y+1];

				if	   (a < b && a > c) result += a;
				else if(a > b && a < c) result -= a;

				if	   (b < a && b > d) result += b;
				else if(b > a && b < d) result -= b;

				if	   (c > a && c < d) result += c;
				else if(c < a && c > d) result -= c;

				if		(d < c && d > b) result += d;
				else if (d > c && d < b) result -= d;
			}
			else{
				LL a = inp[x-1], b = inp[x], c = inp[x+1], b1 = inp[y];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;

				a = inp[y-1]; b = inp[y]; c = inp[y+1]; b1 = inp[x];

				if	   (b1 > a && b1 > c) result += b1;
				else if(b1 < a && b1 < c) result -= b1;

				if	   (a < b && a > b1) result += a;
				else if(a > b && a < b1) result -= a;

				if	   (c < b && c > b1) result += c;
				else if(c > b && c < b1) result -= c;

				if	   (b < a && b < c) result += b;
				else if(b > a && b > c) result -= b;
			}

			swap(inp[x], inp[y]);
			cout << result << "\n";
		}
		for(int i = 0; i <= n; i++) inp[i] = 0;
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;

const int MN = 300003;
LL inp[MN];

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		LL even = 0, odd = 0;
		LL tEven = 0, tOdd = 0;
		for(int i = 1; i <= n; i++){
			cin >> inp[i];
			tOdd = max(odd, even + inp[i]);
			tEven = max(even, odd - inp[i]);
			odd = tOdd;
			even = tEven;
		}

		LL result = max(odd, even);
		cout << result << "\n";

		while(q--){
			int x, y; cin >> x >> y;
			LL a = inp[x-1], b = inp[x], c = inp[x+1], b1 = inp[y];

			if	   (b1 > a && b1 > c) result += b1;
			else if(b1 < a && b1 < c) result -= b1;

			if	   (a < b && a > b1) result += a;
			else if(a > b && a < b1) result -= a;

			if	   (c < b && c > b1) result += c;
			else if(c > b && c < b1) result -= c;

			if	   (b < a && b < c) result += b;
			else if(b > a && b > c) result -= b;

			a = inp[y-1]; b = inp[y]; c = inp[y+1]; b1 = inp[x];

			if	   (b1 > a && b1 > c) result += b1;
			else if(b1 < a && b1 < c) result -= b1;

			if	   (a < b && a > b1) result += a;
			else if(a > b && a < b1) result -= a;

			if	   (c < b && c > b1) result += c;
			else if(c > b && c < b1) result -= c;

			if	   (b < a && b < c) result += b;
			else if(b > a && b > c) result -= b;

			swap(inp[x], inp[y]);

			cout << result << "\n";
		}
	}
	return 0;
}


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

const int MAX = (1 << 19), MN = 300003;
LL mxP[2*MAX], mxN[2*MAX], mnP[2*MAX], mnN[2*MAX];
int mxLP[2*MAX], mxLN[2*MAX], mnLP[2*MAX], mnLN[2*MAX];
int inp[MN];

void Update(int a, int i){
	i += MAX;
	mxN[i] = a;
	mnN[i] = a;
	mxP[i] = 0;
	mnP[i] = 0;
	mxLN[i] = 1;
	mnLN[i] = 1;
	mxLP[i] = 0;
	mnLP[i] = 0;
	i >>= 1;
	while(i > 0){

		if(mxP[i*2] + mxP[i*2+1] < mxN[i*2] - mnN[i*2+1]){
			mxP[i] = mxN[i*2] - mnN[i*2+1];
			mxLP[i] = mxLN[i*2] + mnLN[i*2+1];
		}
		else{
			mxP[i] = mxP[i*2] + mxP[i*2+1];
			mxLP[i] = mxLP[i*2] + mxLP[i*2+1];
		}

		if(mxP[i*2] + mxN[i*2+1] < mxN[i*2] - mnP[i*2+1]){
			mxN[i] = mxN[i*2] - mnP[i*2+1];
			mxLN[i] = mxLN[i*2] + mnLP[i*2+1];
		}
		else{
			mxN[i] = mxP[i*2] + mxN[i*2+1];
			mxLN[i] = mxLP[i*2] + mxLN[i*2+1];
		}

		if(mnP[i*2] + mnP[i*2+1] > mnN[i*2] - mxN[i*2+1]){
			mnP[i] = mnN[i*2] - mxN[i*2+1];
			mnLP[i] = mnLN[i*2] + mxLN[i*2+1];
		}
		else{
			mnP[i] = mnP[i*2] + mnP[i*2+1];
			mnLP[i] = mnLP[i*2] + mnLP[i*2+1];
		}

		if(mnP[i*2] + mnN[i*2+1] > mnN[i*2] - mxP[i*2+1]){
			mnN[i] = mnN[i*2] - mxP[i*2+1];
			mnLN[i] = mnN[i*2] + mxLP[i*2+1];
		}
		else{
			mnN[i] = mnP[i*2] + mnN[i*2+1];
			mnLN[i] = mnLP[i*2] + mnLN[i*2+1];
		}

		i >>= 1;
	}
}

int main(){
	int t; cin >> t;
	while(t--){
		int n, q; cin >> n >> q;
		for(int i = 0; i < n; i++){
			cin >> inp[i];
			Update(inp[i], i);
		}

		cout << max(mxN[1], mxP[1]) << "\n";

		for(int i = 0; i < q; i++){
			int a, b; cin >> a >> b;
			Update(inp[b-1], a-1);
			Update(inp[a-1], b-1);
			swap(inp[a-1], inp[b-1]);
			cout << max(mxN[1], mxP[1]) << "\n";
		}

		for(int i = 0; i < n; i++) Update(0, i);
	}
	return 0;
}


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

long long int m;
long long int arr[1000000+5];

long long int sum_plus=0;
long long int sum_minus=0;
long long int max1=0;

void erase(int i)
{

   if (i==0||i==m+1) return;
    if(arr[i]>arr[i-1] && arr[i] >arr[i+1])
        sum_plus-=arr[i];
    if(arr[i]<arr[i-1] && arr[i]<arr[i+1])
        sum_minus-=arr[i];

}
void insert(int i)
{

    if (i==0||i==m+1) return;
    if(arr[i]>arr[i-1] && arr[i] >arr[i+1])
        sum_plus+=arr[i];
    if(arr[i]<arr[i-1] && arr[i]<arr[i+1])
        sum_minus+=arr[i];

}

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

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int n;
        cin>>m>>n;

        for(int j=1;j<=m;j++){
            cin>>arr[j];

        }
       arr[0]=-1;
        arr[m+1]=-1;

        for(int y=0;y<=n;y++){

            if(y==0){

             for(int k=1;k<=m;k++){

            if(k==1){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }

                    max1=max(arr[k],max1);
            }
            else if(k==m){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                 max1=max(arr[k],max1);
            }
            else{
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                 max1=max(arr[k],max1);
            }

        }

        cout<<max((sum_plus-sum_minus),max1);
        cout<<'\n';

            }

            else{

               int l,r;

            cin>>l>>r;

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

            cout<<max((sum_plus-sum_minus),max1);
            cout<<'\n';

            }

        }

        sum_plus=0;
        sum_minus=0;
        max1=0;
    }
}


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

long long int arr[1000000+5];

void solve(int start,int end){

    long long int max1=0;
    long long int sum_plus=0;
    long long int sum_minus=0;
    for(int k=start;k<end;k++){

            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }

                    max1=max(arr[k],max1);
            }
            else if(k==end-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                 max1=max(arr[k],max1);
            }
            else{
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                 max1=max(arr[k],max1);
            }

        }

        cout<<max((sum_plus-sum_minus),max1);
        cout<<'\n';

}

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

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

            if(y==0){
             solve(0,m);
            }
            else{

              int op[2];

            for(long long int j=0;j<2;j++){
            cin>>op[j];
            }

           swap(arr[op[0]-1],arr[op[1]-1]);

        solve(0,m);
            }

        }

        // sum_plus=0;
        // sum_minus=0;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        long long int arr[m];

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

        long long int max=0;
        long long int sum_plus=0;
        long long int sum_minus=0;

        for(int k=0;k<m;k++){
            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k==m-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k!=0 && k!=m-1){
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                if(arr[k]>=max)
                    max=arr[k];
            }

        }

        if((sum_plus-sum_minus)>max){
            cout<<(sum_plus-sum_minus);
            cout<<'\n';
        }
        else{
            cout<<max;
            cout<<'\n';
        }

            if(y!=n){
            long long int op[2];

            for(int j=0;j<2;j++){
            cin>>op[j];

        }

            long long int temp=arr[op[0]-1];
            arr[op[0]-1]=arr[op[1]-1];
            arr[op[1]-1]=temp;
            }

        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        long long int arr[m];

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

        long long int max1=0;
        long long int sum_plus=0;
        long long int sum_minus=0;

        for(int k=0;k<m;k++){
            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }

                    max1=max(arr[k],max1);
            }
            else if(k==m-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                 max1=max(arr[k],max1);
            }
            else{
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                 max1=max(arr[k],max1);
            }

        }

        cout<<max((sum_plus-sum_minus),max1);
        cout<<'\n';

            if(y!=n){
            long long int op[2];

            for(int j=0;j<2;j++){
            cin>>op[j];

        }

           swap(arr[op[0]-1],arr[op[1]-1]);

            }

        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        long long int arr[m];

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

        long long int max1=0;
        long long int sum_plus=0;
        long long int sum_minus=0;

        for(int k=0;k<m;k++){
            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }
                if(arr[k]>=max1)
                    max1=arr[k];
            }

            if(k==m-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                if(arr[k]>=max1)
                    max1=arr[k];
            }

            if(k!=0 && k!=m-1){
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                if(arr[k]>=max1)
                    max1=arr[k];
            }

        }

        cout<<max((sum_plus-sum_minus),max1);
        cout<<'\n';

            if(y!=n){
            long long int op[2];

            for(int j=0;j<2;j++){
            cin>>op[j];

        }

           swap(arr[op[0]-1],arr[op[1]-1]);

            }

        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        long long int arr[m];

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

        long long int max=0;
        long long int sum_plus=0;
        long long int sum_minus=0;

        for(int k=0;k<m;k++){
            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k==m-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k!=0 && k!=m-1){
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                if(arr[k]>=max)
                    max=arr[k];
            }

        }

        if((sum_plus-sum_minus)>max){
            cout<<(sum_plus-sum_minus);
            cout<<'\n';
        }
        else{
            cout<<max;
            cout<<'\n';
        }

            if(y!=n){
            long long int op[2];

            for(int j=0;j<2;j++){
            cin>>op[j];

        }

            long long int temp=arr[op[0]-1];
            arr[op[0]-1]=arr[op[1]-1];
            arr[op[1]-1]=temp;
            }

        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long long int p;
    cin>>p;

    for(int i=0;i<p;i++){
        long long int m,n;
        cin>>m>>n;

        long long int arr[m];

        for(int j=0;j<m;j++){
            cin>>arr[j];

        }

        for(int y=0;y<=n;y++){

        long long int max=0;
        long long int sum_plus=0;
        long long int sum_minus=0;

        for(int k=0;k<m;k++){
            if(k==0){
                if(arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];

                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k==m-1){
                if(arr[k]>=arr[k-1]){
                    sum_plus+=arr[k];
                }
                if(arr[k]>=max)
                    max=arr[k];
            }

            if(k!=0 && k!=m-1){
                if(arr[k]<=arr[k-1] && arr[k]<=arr[k+1]){
                    sum_minus+=arr[k];
                }
                else if(arr[k]>=arr[k-1] && arr[k]>=arr[k+1]){
                    sum_plus+=arr[k];
                }

                if(arr[k]>=max)
                    max=arr[k];
            }

        }

        if((sum_plus-sum_minus)>max){
            cout<<(sum_plus-sum_minus);
            cout<<'\n';
        }
        else{
            cout<<max;
            cout<<'\n';
        }

            if(y!=n){
            long long int op[2];

            for(int j=0;j<2;j++){
            cin>>op[j];

        }

            swap(arr[op[0]-1],arr[op[1]-1]);
            // long long int temp=arr[op[0]-1];
            // arr[op[0]-1]=arr[op[1]-1];
            // arr[op[1]-1]=temp;
            }

        }
    }
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int inf = 1LL * 1e18 + 10;

const int N = 3e5 + 10;

// 1 1
// 1 -1
// -1 1
// -1 -1

int a[N];

int st[N * 4][2][2];

void relax(int p) {

    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
            st[p][x][y] = max(st[p * 2][x][y], st[p * 2 + 1][x][y]);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (i == j) continue;
                    st[p][x][y] = max(st[p][x][y], st[p * 2][x][i] + st[p * 2 + 1][j][y]);
                }
            }
        }
    }

}

void build(int p, int l, int r) {
    if (l == r) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }
    int m = (l + r) / 2;
    build(p * 2, l, m);
    build(p * 2 + 1, m + 1, r);
    relax(p);
}

void update(int p, int l, int r, int L, int R) {
    if (r < L || l > R) {
        return;
    }
    if (l >= L && r <= R) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }

    int m = (l + r) / 2;
    update(p * 2, l, m, L, R);
    update(p * 2 + 1, m + 1, r, L, R);
    relax(p);
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    build(1, 0, n - 1);
    cout << st[1][0][0] << "\n";
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        swap(a[l], a[r]);
        update(1, 0, n - 1, l, l);
        update(1, 0, n - 1, r, r);
        cout << st[1][0][0] << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(0);

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

    return 0;
}


#include <bits/stdc++.h>

// #define int long long

using namespace std;

const int inf = 1e9 + 10;

const int N = 3e5 + 10;

// 1 1
// 1 -1
// -1 1
// -1 -1

int a[N];

int st[N * 4][2][2];

void relax(int p) {

    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
            st[p][x][y] = max(st[p * 2][x][y], st[p * 2 + 1][x][y]);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (i == j) continue;
                    st[p][x][y] = max(st[p][x][y], st[p * 2][x][i] + st[p * 2 + 1][j][y]);
                }
            }
        }
    }

}

void build(int p, int l, int r) {
    if (l == r) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }
    int m = (l + r) / 2;
    build(p * 2, l, m);
    build(p * 2 + 1, m + 1, r);
    relax(p);
}

void update(int p, int l, int r, int L, int R) {
    if (r < L || l > R) {
        return;
    }
    if (l >= L && r <= R) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }

    int m = (l + r) / 2;
    update(p * 2, l, m, L, R);
    update(p * 2 + 1, m + 1, r, L, R);
    relax(p);
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    build(1, 0, n - 1);
    cout << st[1][0][0] << "\n";
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        swap(a[l], a[r]);
        update(1, 0, n - 1, l, l);
        update(1, 0, n - 1, r, r);
        cout << st[1][0][0] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);

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

    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int inf = 1LL * 1e18 + 10;
const int N = 3e5 + 10;

int a[N];
int st[N * 4][2][2];

void relax(int p) {
    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
            st[p][x][y] = max(st[p * 2][x][y], st[p * 2 + 1][x][y]);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (i == j) continue;
                    st[p][x][y] = max(st[p][x][y], st[p * 2][x][i] + st[p * 2 + 1][j][y]);
                }
            }
        }
    }
}

void update(int p, int l, int r, int L, int R) {
    if (r < L || l > R) return;
    if (l >= L && r <= R) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }
    int m = (l + r) / 2;
    update(p * 2, l, m, L, R);
    update(p * 2 + 1, m + 1, r, L, R);
    relax(p);
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n * 4; i++) {
        st[i][0][0] = 0;
        st[i][0][1] = -inf;
        st[i][1][0] = -inf;
        st[i][1][1] = inf;

    }
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        update(1, 0, n - 1, i, i);
    }
    cout << st[1][0][0] << "\n";
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        swap(a[l], a[r]);
        update(1, 0, n - 1, l, l);
        update(1, 0, n - 1, r, r);
        cout << st[1][0][0] << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    int tt;
    cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

// #define int long long

using namespace std;

const int inf = 1e9 + 10;

const int N = 3e5 + 10;

// 1 1
// 1 -1
// -1 1
// -1 -1

long long a[N];

long long st[N * 4][2][2];

void relax(int p) {

    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
            st[p][x][y] = max(st[p * 2][x][y], st[p * 2 + 1][x][y]);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (i == j) continue;
                    st[p][x][y] = max(st[p][x][y], st[p * 2][x][i] + st[p * 2 + 1][j][y]);
                }
            }
        }
    }

}

void build(int p, int l, int r) {
    if (l == r) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }
    int m = (l + r) / 2;
    build(p * 2, l, m);
    build(p * 2 + 1, m + 1, r);
    relax(p);
}

void update(int p, int l, int r, int L, int R) {
    if (r < L || l > R) {
        return;
    }
    if (l >= L && r <= R) {
        st[p][0][0] = a[l];
        st[p][0][1] = -inf;
        st[p][1][0] = -inf;
        st[p][1][1] = -a[l];
        return;
    }

    int m = (l + r) / 2;
    update(p * 2, l, m, L, R);
    update(p * 2 + 1, m + 1, r, L, R);
    relax(p);
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    build(1, 0, n - 1);
    cout << st[1][0][0] << "\n";
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        swap(a[l], a[r]);
        update(1, 0, n - 1, l, l);
        update(1, 0, n - 1, r, r);
        cout << st[1][0][0] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);

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

    return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
int main()
{
  fast
//   #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
//   #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;
	   ll a[n+2];
	   a[0]=-100007;
	   a[n+1]=0ll;
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   int f=0,res=0;
	    for(ll i=1;i<=n;i++)
		{
			if(a[i]-a[i+1]>=0)
			  res+=(a[i]-a[i+1]);
		}

		cout<<res<<nl;

		for(ll k=1;k<=q;k++)
		{
			ll l,r;scanf("%lld %lld",&l,&r);
			// Two cases
			// 1. if l and r are not consecutive:
			if(abs(r-l)>1)
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[l]-a[l+1]>=0)
				  res=res-(a[l]-a[l+1]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);
					// cout<<res<<nl;
				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;

				  if(a[l-1]-a[l]>=0)
				  res=res+a[l-1]-a[l];
				if(a[r-1]-a[r]>=0)
				  res=res+a[r-1]-a[r];
				  if(a[l]-a[l+1]>=0)
				  res=res+a[l]-a[l+1];
				  if(a[r]-a[r+1]>=0)
				  res=res+a[r]-a[r+1];

          cout<<res<<nl;

			}
			// 2. if l and r are consecutive:
			else
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);

				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;
				if(a[l-1]-a[l]>=0)
				  res=res+(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res+(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res+(a[r]-a[r+1]);

				  cout<<res<<nl;

			}

		}
   }

  return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
int main()
{
  fast
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;
	   ll a[n+2];
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   a[n+1]=-7;
	   a[0]=-7;
	   ll f=0,res=0;
	   for(ll i=2;i<=n;i++)
	   {
		//    cout<<"debug";
		   if(a[i]>=curr && f==0)
		       {
				   curr=a[i];
				   continue;
			   }
		   if(a[i]<=curr && f==1)
		   {
			   curr=a[i];
			   continue;
		   }
		   if(a[i]<curr && f==0)
		   {
                 res+=curr;
				 f=1;
				 curr=a[i];
				 continue;
		   }
		   if(a[i]>curr && f==1)
		   {
			   res-=curr;
			   curr=a[i];
			   f=0;
			   continue;
		   }
	   }
	   if(f==0)
	     res+=curr;
		cout<<res<<nl;

		for(ll i=1;i<=q;i++)
		{
			ll l,r;
			cin>>l>>r;
			if(l==r)
			{
			  cout<<res<<nl;
			  continue;
			}
			if(l-r==-1)
			{
				if(a[l]>a[l-1] && a[l]>a[l+1])
				   res-=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res+=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res-=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res+=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				 {

				   res-=a[r];}
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res+=a[r];

				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res-=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res+=a[r+1];
				 }

				 swap(a[l],a[r]);
				 if(a[l]>a[l-1] && a[l]>a[l+1])
				   res+=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res-=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res+=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res-=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res+=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res-=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res+=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res-=a[r+1];
				 }
				cout<<res<<nl;
				continue;
			}
			if(a[l]>a[l-1] && a[l]>a[l+1])
				   res-=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res+=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res-=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res+=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res-=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res+=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res-=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res+=a[r+1];
				 }
				  if(a[l+1]>a[l+2] && a[l+1]>a[l])
				   res-=a[l+1];
				if(a[l+1]<a[l] && a[l+1]<a[l+2])
				 res+=a[l+1];
				  if(a[r-1]>a[r-2] && a[r-1]>a[r])
				   res-=a[r-1];
				if(a[r-1]<a[r-2] && a[r-1]<a[r])
				 res+=a[r-1];

				 swap(a[l],a[r]);
			if(a[l]>a[l-1] && a[l]>a[l+1])
				   res+=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res-=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res+=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res-=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res+=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res-=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res+=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res-=a[r+1];
				 }
				  if(a[l+1]>a[l+2] && a[l+1]>a[l])
				   res+=a[l+1];
				if(a[l+1]<a[l] && a[l+1]<a[l+2])
				 res-=a[l+1];
				  if(a[r-1]>a[r-2] && a[r-1]>a[r])
				   res+=a[r-1];
				if(a[r-1]<a[r-2] && a[r-1]<a[r])
				 res-=a[r-1];

			cout<<res<<nl;
		}
   }

  return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
int main()
{
  fast
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;
	   ll a[n+2];
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   a[n+1]=-7;
	   a[0]=-7;
	   int f=0,res=0;
	   for(ll i=2;i<=n;i++)
	   {
		//    cout<<"debug";
		   if(a[i]>=curr && f==0)
		       {
				   curr=a[i];
				   continue;
			   }
		   if(a[i]<=curr && f==1)
		   {
			   curr=a[i];
			   continue;
		   }
		   if(a[i]<curr && f==0)
		   {
                 res+=curr;
				 f=1;
				 curr=a[i];
				 continue;
		   }
		   if(a[i]>curr && f==1)
		   {
			   res-=curr;
			   curr=a[i];
			   f=0;
			   continue;
		   }
	   }
	   if(f==0)
	     res+=curr;
		cout<<res<<nl;

		for(ll i=1;i<=q;i++)
		{
			ll l,r;
			cin>>l>>r;
			if(l==r)
			{
			  cout<<res<<nl;
			  continue;
			}
			if(l-r==-1)
			{
				if(a[l]>a[l-1] && a[l]>a[l+1])
				   res-=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res+=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res-=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res+=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				 {

				   res-=a[r];}
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res+=a[r];

				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res-=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res+=a[r+1];
				 }

				 swap(a[l],a[r]);
				 if(a[l]>a[l-1] && a[l]>a[l+1])
				   res+=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res-=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res+=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res-=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res+=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res-=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res+=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res-=a[r+1];
				 }
				cout<<res<<nl;
				continue;
			}
			if(a[l]>a[l-1] && a[l]>a[l+1])
				   res-=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res+=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res-=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res+=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res-=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res+=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res-=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res+=a[r+1];
				 }
				  if(a[l+1]>a[l+2] && a[l+1]>a[l])
				   res-=a[l+1];
				if(a[l+1]<a[l] && a[l+1]<a[l+2])
				 res+=a[l+1];
				  if(a[r-1]>a[r-2] && a[r-1]>a[r])
				   res-=a[r-1];
				if(a[r-1]<a[r-2] && a[r-1]<a[r])
				 res+=a[r-1];

				 swap(a[l],a[r]);
			if(a[l]>a[l-1] && a[l]>a[l+1])
				   res+=a[l];
				if(a[l]<a[l-1] && a[l]<a[l+1])
				 res-=a[l];
				 if(l>1)
				 {
					 if(a[l-1]>a[l-2] && a[l-1]>a[l])
				   res+=a[l-1];
				if(a[l-1]<a[l-2] && a[l-1]<a[l])
				 res-=a[l-1];
				 }
				 if(a[r]>a[r-1] && a[r]>a[r+1])
				   res+=a[r];
				if(a[r]<a[r-1] && a[r]<a[r+1])
				 res-=a[r];
				 if(r<n)
				 {
					  if(a[r+1]>a[r+2] && a[r+1]>a[r])
				   res+=a[r+1];
				if(a[r+1]<a[r] && a[r+1]<a[r+2])
				 res-=a[r+1];
				 }
				  if(a[l+1]>a[l+2] && a[l+1]>a[l])
				   res+=a[l+1];
				if(a[l+1]<a[l] && a[l+1]<a[l+2])
				 res-=a[l+1];
				  if(a[r-1]>a[r-2] && a[r-1]>a[r])
				   res+=a[r-1];
				if(a[r-1]<a[r-2] && a[r-1]<a[r])
				 res-=a[r-1];

			cout<<res<<nl;
		}
   }

  return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
ll a[5000003];
int main()
{
  fast
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;

	   a[0]=-100007;
	   a[n+1]=0ll;
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   ll f=0,res=0;
	    for(ll i=1;i<=n;i++)
		{
			res=res+max(0ll,a[i]-a[i+1]);
		}

		cout<<res<<nl;

		for(ll k=1;k<=q;k++)
		{
			ll l,r;scanf("%lld %lld",&l,&r);
			// Two cases
			// 1. if l and r are not consecutive:
			if(abs(r-l)>1)
			{
				if(a[l-1]-a[l]>=0ll)
				  res=res-(a[l-1]-a[l]);
				if(a[r-1]-a[r]>=0ll)
				  res=res-(a[r-1]-a[r]);
				  if(a[l]-a[l+1]>=0ll)
				  res=res-(a[l]-a[l+1]);
				  if(a[r]-a[r+1]>=0ll)
				  res=res-(a[r]-a[r+1]);
					// cout<<res<<nl;
				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;

				  if(a[l-1]-a[l]>=0ll)
				  res=res+a[l-1]-a[l];
				if(a[r-1]-a[r]>=0ll)
				  res=res+a[r-1]-a[r];
				  if(a[l]-a[l+1]>=0ll)
				  res=res+a[l]-a[l+1];
				  if(a[r]-a[r+1]>=0ll)
				  res=res+a[r]-a[r+1];

          cout<<res<<nl;

			}
			// 2. if l and r are consecutive:
			else
			{
				if(a[l-1]-a[l]>=0ll)
				  res=res-(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0ll)
				  res=res-(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0ll)
				  res=res-(a[r]-a[r+1]);

				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;
				if(a[l-1]-a[l]>=0ll)
				  res=res+(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0ll)
				  res=res+(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0ll)
				  res=res+(a[r]-a[r+1]);

				  cout<<res<<nl;

			}

		}
   }

  return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
ll a[5000003];
int main()
{
  fast
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;

	   a[0]=-100007;
	   a[n+1]=0ll;
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   int f=0,res=0;
	    for(ll i=1;i<=n;i++)
		{
			res=res+max(0ll,a[i]-a[i+1]);
		}

		cout<<res<<nl;

		for(ll k=1;k<=q;k++)
		{
			ll l,r;scanf("%lld %lld",&l,&r);
			// Two cases
			// 1. if l and r are not consecutive:
			if(abs(r-l)>1)
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[l]-a[l+1]>=0)
				  res=res-(a[l]-a[l+1]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);
					// cout<<res<<nl;
				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;

				  if(a[l-1]-a[l]>=0)
				  res=res+a[l-1]-a[l];
				if(a[r-1]-a[r]>=0)
				  res=res+a[r-1]-a[r];
				  if(a[l]-a[l+1]>=0)
				  res=res+a[l]-a[l+1];
				  if(a[r]-a[r+1]>=0)
				  res=res+a[r]-a[r+1];

          cout<<res<<nl;

			}
			// 2. if l and r are consecutive:
			else
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);

				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;
				if(a[l-1]-a[l]>=0)
				  res=res+(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res+(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res+(a[r]-a[r+1]);

				  cout<<res<<nl;

			}

		}
   }

  return 0;
}


// Pranav Singhal (IIIT-ALLAHABAD)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define nl "\n"
int main()
{
  fast
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
   ll t;cin>>t;
   while(t--)
   {
	   ll n;cin>>n;
	   ll q;cin>>q;
	   ll a[n+4];
	   a[0]=-100007;
	   a[n+1]=0ll;
	   for(ll i=1;i<=n;i++) cin>>a[i];
	   ll curr=a[1];
	   int f=0,res=0;
	    for(ll i=1;i<n;i++)
		{
			if(a[i]-a[i+1]>=0)
			  res+=a[i]-a[i+1];
		}
		res+=a[n];

		cout<<res<<nl;

		for(ll k=1;k<=q;k++)
		{
			ll l,r;scanf("%lld %lld",&l,&r);
			// Two cases
			// 1. if l and r are not consecutive:
			if(abs(r-l)>1)
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[l]-a[l+1]>=0)
				  res=res-(a[l]-a[l+1]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);
					// cout<<res<<nl;
				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;

				  if(a[l-1]-a[l]>=0)
				  res=res+a[l-1]-a[l];
				if(a[r-1]-a[r]>=0)
				  res=res+a[r-1]-a[r];
				  if(a[l]-a[l+1]>=0)
				  res=res+a[l]-a[l+1];
				  if(a[r]-a[r+1]>=0)
				  res=res+a[r]-a[r+1];

          cout<<res<<nl;

			}
			// 2. if l and r are consecutive:
			else
			{
				if(a[l-1]-a[l]>=0)
				  res=res-(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res-(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res-(a[r]-a[r+1]);

				  ll temp=a[l];
				  a[l]=a[r];
				  a[r]=temp;
				if(a[l-1]-a[l]>=0)
				  res=res+(a[l-1]-a[l]);
				  if(a[r-1]-a[r]>=0)
				  res=res+(a[r-1]-a[r]);
				  if(a[r]-a[r+1]>=0)
				  res=res+(a[r]-a[r+1]);

				  cout<<res<<nl;

			}

		}
   }

  return 0;
}


#include <iostream>
//#include "buble_sort.h"

using namespace std;
//void sorting (int x[]);
void bublesort(int *x,int n)
{
    int k=1,flag=0,i=0,temp;
    for (k=1; k<=n-1; k++)
    {
        flag=0;
        for(i=0; i<=n-k-1; i++)
        {
            if (x[i]<x[i+1]){
                temp=x[i+1];
                x[i+1]=x[i];
                x[i]=temp;
                flag=1;
            }

        }
        if (flag==0)break;
    }

}

int main()
{
    int n,i=0,Sereja=0,Dima=0,k;
     cin>>n;
    int x[1001]={0};
    for (i=0; i<n; i++)cin>>x[i];
 //   bublesort(x,n);
    //for (i=0; i<n; i++)cout<<"   "<<x[i]<<"   ";
    i=0;
    k=0;
    while(i<n){
        if(k==0){
                if (x[i]>x[n-1]){
                    Dima=Dima+x[i];
                    i++;
                }
                else{
                        Dima=Dima+x[n-1];
                n--;
                }
                k=1;
        }
        else{
                 if (x[i]>x[n-1]){
                    Sereja=Sereja+x[i];
                    i++;
                }
                else{
                        Sereja=Sereja+x[n-1];
                n--;
                }
                k=0;
        }
    }
//
//    for (i=0;i<n;i=i+2){
//        Dima=Dima+x[i];
//                        Sereja=Sereja+x[i+1];
//    }
    cout<<Dima<<"  ";
    cout<<Sereja;

   // cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
//#include "buble_sort.h"

using namespace std;
//void sorting (int x[]);
void bublesort(int *x,int n)
{
    int k=1,flag=0,i=0,temp;
    for (k=1; k<=n-1; k++)
    {
        flag=0;
        for(i=0; i<=n-k-1; i++)
        {
            if (x[i]<x[i+1]){
                temp=x[i+1];
                x[i+1]=x[i];
                x[i]=temp;
                flag=1;
            }

        }
        if (flag==0)break;
    }

}

int main()
{
    int n,i=0,Sereja=0,Dima=0,k;
     cin>>n;
    int x[1001]={0};
    for (i=0; i<n; i++)cin>>x[i];
 //   bublesort(x,n);
    //for (i=0; i<n; i++)cout<<"   "<<x[i]<<"   ";
    i=0;
    k=0;
    while(i<n){
        if(k==0){
                if (x[i]>x[n-1]){
                    Dima=Dima+x[i];
                    i++;
                }
                else{
                        Dima=Dima+x[n-1];
                n--;
                }
                k=1;
        }
        else{
                 if (x[i]>x[n-1]){
                    Sereja=Sereja+x[i];
                    i++;
                }
                else{
                        Sereja=Sereja+x[n-1];
                n--;
                }
                k=0;
        }
    }
//
//    for (i=0;i<n;i=i+2){
//        Dima=Dima+x[i];
//                        Sereja=Sereja+x[i+1];
//    }
    cout<<max(Dima,Sereja)<<"  ";
    cout<<min(Dima,Sereja);

   // cout << "Hello world!" << endl;
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
				int a[1004],n,i,j,ca=0,cb=0,m,pos;
				scanf("%d",&n);
				for(i=0; i<n; i++)
				    scanf("%d",&a[i]);
				sort(a,a+n);
				for(i=n-1; i>=0; i=i-2)
				    ca = ca + a[i];
				for(i=n-2; i>=0; i=i-2)
				    cb = cb + a[i];
				printf("%d %d\n",ca,cb);

				return 0;
}


#include<stdio.h>
int main()
{
				int a[1004],n,i,j,ca=0,cb=0,m,pos;
				scanf("%d",&n);
				for(i=0; i<n; i++)
				    scanf("%d",&a[i]);
				for(i=0; i<n; i++) {
								m = 0;
								for(j=0; j<n; j++) {
												if(a[j]>m) {
												    m = a[j];
												    pos = j;
												}
								}
								//printf("%d %d\n",m,pos);
								if(i%2==0)
												    ca = ca + m;
												else
												    cb = cb + m;
								a[pos] = 0;
				}

				printf("%d %d\n",ca,cb);

				return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
				int a[1004],n,i,j,ca=0,cb=0,m,pos;
				scanf("%d",&n);
				for(i=0; i<n; i++)
				    scanf("%d",&a[i]);
				sort(a,a+n);
				for(i=0; i<n; i=i+2)
				    ca = ca + a[i];
				for(i=1; i<n; i=i+2)
				    cb = cb + a[i];
				printf("%d %d\n",ca,cb);

				return 0;
}


#include<stdio.h>
using namespace std;

int main()
{
    int a[1004],i,j,n,s=0,d=0,l,r;
    scanf("%d",&n);

    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    l=0;
    r=n-1;
    for(i=0; i<n; i++) {
        j=0;
        if(a[l] >= a[r]) {
            j=a[l];
            l++;
        }
        else if(a[l] < a[r]) {
            j = a[r];
            r--;
        }

        if(i%2==0)
            s = s + j;
        else
            d = d + j;
        //printf("%d ",j);
    }

    printf("%d %d\n",s,d);

    return 0;
}


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

int arr[1001];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	int s = 0 , d=0;
	for(int i=0;i<n;i++)
	{
		if(i%2)d+=arr[i];
		else s+=arr[i];
	}
	cout<<d<<" "<<s<<endl;
	return 0;
}


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

int arr[1001];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	int s = 0 , d=0;
	for(int i=0;i<n;i++)
	{
		if(i%2)d+=arr[i];
		else s+=arr[i];
	}
	cout<<s<<" "<<d<<endl;
	return 0;
}


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

int arr[1001];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	int s = 0 , d=0;
	for(int i=0;i<n;i++)
	{
		if(i%2)d+=arr[i];
		else s+=arr[i];
	}
	cout<<max(d,s)<<" "<<min(d,s)<<endl;
	return 0;
}


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

int arr[1001];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>arr[i];
	int s = 0 , d=0;
	bool turn = true;
	int i=0 , j=n-1;
	while(i<=j)
	{
		if(turn)
		{
			if(arr[i]>=arr[j])
			{
				s = s + arr[i++];
			}
			else s = s + arr[j--];
			turn = !turn;
		}
		else
		{
			turn = !turn;
			if(arr[i]>=arr[j])
			{
				d = d + arr[i++];
			}
			else d = d + arr[j--];
		}
	}
	cout<<s<<" "<<d<<endl;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int sizze,arr[1000],serejaSum=0,DimaSum=0,temp,current=0,total=0;
    cin>>sizze;
    for(int i=0;i<sizze;i++)
        {cin>>arr[i];total+=arr[i];}
        /*SOrting the array of costs*/
    for(int i=1;i<sizze;i++){
            temp=arr[i];
            current=i;
        for(int x=i-1;x>=0;x--){
            if(temp<arr[x])
            {   arr[current]=arr[x];
                arr[x]=temp;
                current=x;
            }
            }
                    if(sizze%2==1 && i%2==1)
                        {
                            DimaSum+=arr[i];
                                  }
                   else if(sizze%2==0 && i%2==0)
                            {
                                DimaSum+=arr[i];
                                   }

    }
    if(sizze%2==0)
        {
serejaSum=total-DimaSum-1;
DimaSum+=arr[0];
    }
    else
        {
                serejaSum=total-DimaSum;
        }
cout<< serejaSum<<" "<<DimaSum;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int sizze,magnetsNo[100000],groupOfMagnets=0;
    cin>>sizze;
    for(int i=0;i<sizze;i++)
        cin>>magnetsNo[i];
    for(int i=0;i<sizze;i++){
        if(magnetsNo[i]!=magnetsNo[i+1])
            groupOfMagnets++;
    }
    cout<< groupOfMagnets;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int sizze,arr[1000],serejaSum=0,DimaSum=0,left=0,right=0;
    cin>>sizze;
    for(int i=0;i<sizze;i++)
        cin>>arr[i];
        left=0;right=sizze-1;
    for(int i=0;i<sizze;i++){
        if(i%2==0)
        {
           if(arr[left]>arr[right] && right>left)
           {serejaSum+=arr[left];left++;}
           else if(arr[left]<arr[right] && right>left)
           {serejaSum+=arr[right];right--;}
        else if(right==left)
           {serejaSum+=arr[right];right--;}
        }
        else
        {
           if(arr[left]>arr[right] && right>left)
           {DimaSum+=arr[left];left++;}
           else if(arr[left]<arr[right] && right>left)
           {DimaSum+=arr[right];right--;}
            else if(right==left)
           {DimaSum+=arr[right];right--;}
        }

    }
    cout<<serejaSum<<" "<<DimaSum;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int sizze,arr[1000],serejaSum=0,DimaSum=0,temp,current=0,total=0;
    cin>>sizze;
    for(int i=0;i<sizze;i++)
        {cin>>arr[i];total+=arr[i];}
        /*SOrting the array of costs*/
    for(int i=1;i<sizze;i++){
            temp=arr[i];
            current=i;
        for(int x=i-1;x>=0;x--){
                    if(temp<arr[x])
                    {   arr[current]=arr[x];
                        arr[x]=temp;
                        current=x;
                    }
        }
    }
for(int i=0;i<sizze;i++){
    if(sizze%2==1 && i%2==1)
                        {
                            DimaSum+=arr[i];
                                  }
    if(sizze%2==1 && i%2==0)
                        {
                            serejaSum+=arr[i];
                                  }
     if(sizze%2==0 && i%2==0)
                            {
                                DimaSum+=arr[i];
                                   }
     if(sizze%2==0 && i%2==1)
                            {
                                serejaSum+=arr[i];
                                   }

}
cout<< serejaSum<<" "<<DimaSum;
/*for(int i=0;i<sizze;i++)
    cout<<arr[i]<<" ";*/
    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="A. Sereja and Dima" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/A. Sereja and Dima" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/A. Sereja and Dima" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <iostream>

using namespace std;

int main()
{
    int sizze,arr[1000],serejaSum=0,DimaSum=0,temp,current=0,total=0;
    cin>>sizze;
    for(int i=0;i<sizze;i++)
        {cin>>arr[i];total+=arr[i];}
        /*SOrting the array of costs*/
    for(int i=1;i<sizze;i++){
            temp=arr[i];
            current=i;
        for(int x=i-1;x>=0;x--){
                    if(temp<arr[x])
                    {   arr[current]=arr[x];
                        arr[x]=temp;
                        current=x;
                    }
        }
    }
for(int i=0;i<sizze;i++){
    if(sizze%2==1 && i%2==1)
                        {
                            DimaSum+=arr[i];
                                  }
                   else if(sizze%2==0 && i%2==0)
                            {
                                DimaSum+=arr[i];
                                   }

}
serejaSum=total-DimaSum;
cout<< serejaSum<<" "<<DimaSum;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <utility>
using namespace std;

int main(){
    int n;
    cin >> n;
    int a[2000];
    int all = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        all += a[i];
    }
    int vis = 0;
    int p = 0;
    int sum = 0;
    for (int i = 0, j = n - 1;i <= j;)
    {
        if (i == j && n % 2 != 0)
        {
            sum += a[i];
            break;
        }
        else
            break;
        vis++;
        if (a[i] > a[j])
        {
            if (vis % 2 != 0)
            sum += a[i];
            i++;
        }
        else if (a[i] < a[j])
        {
            if (vis % 2 != 0)
            sum += a[j];
            j--;
        }
        else
        {
            int ok = 1;
            for (int ii = i, jj = j; ii != jj; ii++,jj++)
            {
                if(a[ii] > a[jj])
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                    ok = 0;
                    break;
                }
                if(a[jj] > a[ii])
                {
                    if (vis % 2 != 0)
                    sum += a[jj];
                    j++;
                    ok = 0;
                    break;
                }
                if(ok)
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                }
            }
        }
    }
        cout << sum << " " << all - sum;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <utility>
using namespace std;

int main(){
    int n;
    cin >> n;
    int a[2000];
    int all = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        all += a[i];
    }
    int vis = 0;
    int p = 0;
    int sum = 0;
    for (int i = 0, j = n - 1;i <= j;)
    {
        if (i == j && n % 2 != 0)
        {
            sum += a[i];
            break;
        }
        vis++;
        if (a[i] > a[j])
        {
            if (vis % 2 != 0)
            sum += a[i];
            i++;
        }
        else if (a[i] < a[j])
        {
            if (vis % 2 != 0)
            sum += a[j];
            j--;
        }
        else
        {
            int ok = 1;
            for (int ii = i, jj = j; ii != jj; ii++,jj++)
            {
                if(a[ii] > a[jj])
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                    ok = 0;
                    break;
                }
                if(a[jj] > a[ii])
                {
                    if (vis % 2 != 0)
                    sum += a[jj];
                    j++;
                    ok = 0;
                    break;
                }
                if(ok)
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                }
            }
        }
    }
        cout << sum << " " << all - sum;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <utility>
using namespace std;

int main(){
    int n;
    cin >> n;
    int a[2000];
    int all = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        all += a[i];
    }
    int vis = 0;
    int p = 0;
    int sum = 0;
    for (int i = 0, j = n - 1;i <= j;)
    {
        if (i == j)
        {
            sum += a[i];
            break;
        }
        vis++;
        if (a[i] > a[j])
        {
            if (vis % 2 != 0)
            sum += a[i];
            i++;
        }
        else if (a[i] < a[j])
        {
            if (vis % 2 != 0)
            sum += a[j];
            j--;
        }
        else
        {
            int ok = 1;
            for (int ii = i, jj = j; ii != jj; ii++,jj++)
            {
                if(a[ii] > a[jj])
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                    ok = 0;
                    break;
                }
                if(a[jj] > a[ii])
                {
                    if (vis % 2 != 0)
                    sum += a[jj];
                    j++;
                    ok = 0;
                    break;
                }
                if(ok)
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                }
            }
        }
    }
        cout << sum << " " << all - sum;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <utility>
using namespace std;

int main(){
    int n;
    cin >> n;
    int a[2000];
    int all = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        all += a[i];
    }
    int vis = 0;
    int p = 0;
    int sum = 0;
    for (int i = 0, j = n - 1;i <= j;)
    {
        if (i == j && n % 2 != 0)
        {
            sum += a[i];
            break;
        }
        else if (i == j)
            break;
        vis++;
        if (a[i] > a[j])
        {
            if (vis % 2 != 0)
            sum += a[i];
            i++;
        }
        else if (a[i] < a[j])
        {
            if (vis % 2 != 0)
            sum += a[j];
            j--;
        }
        else
        {
            int ok = 1;
            for (int ii = i, jj = j; ii != jj; ii++,jj++)
            {
                if(a[ii] > a[jj])
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                    ok = 0;
                    break;
                }
                if(a[jj] > a[ii])
                {
                    if (vis % 2 != 0)
                    sum += a[jj];
                    j++;
                    ok = 0;
                    break;
                }
                if(ok)
                {
                    if (vis % 2 != 0)
                    sum += a[i];
                    i++;
                }
            }
        }
    }
        cout << sum << " " << all - sum;
    return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define pii pair<int, int>
#define VI vector < int >
#define PB push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORD(i,a,b) for(i=a;i>b;i--)
typedef long long LL;
using namespace std;
int main()
{
	int i,j,k,n,t;
	int a[1000000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	int sum1=0,sum2=0;
	int end=n-1,start=0,c=0;
	while(start<=end)
	{
		int add;
//		cout<<start<<" "<<end<<endl;
		if(start>end)
		{
			start++;
			add=start-1;
		}
		else
		{
			end--;
			add=end+1;
		}
//		cout<<a[add]<<endl;
		if(c%2==0)
			sum1+=a[add];
		else
			sum2+=a[add];
		c++;
	}
/*	for(i=n-1;i>=0;i-=2)
	sum1+=a[i];
	for(i=n-2;i>=0;i-=2)
	sum2+=a[i];*/
	cout<<sum1<<" "<<sum2<<endl;
	return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define pii pair<int, int>
#define VI vector < int >
#define PB push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORD(i,a,b) for(i=a;i>b;i--)
typedef long long LL;
using namespace std;
int main()
{
	int i,j,k,n,t;
	int a[1000000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	int sum1=0,sum2=0;
	int end=n-1,start=0,c=0;
	while(start<=end)
	{
		int add;
		cout<<start<<" "<<end<<endl;
		if(a[start]>a[end])
		{
			start++;
			add=start-1;
		}
		else
		{
			end--;
			add=end+1;
		}
		cout<<a[add]<<endl;
		if(c%2==0)
			sum1+=a[add];
		else
			sum2+=a[add];
		c++;
	}
/*	for(i=n-1;i>=0;i-=2)
	sum1+=a[i];
	for(i=n-2;i>=0;i-=2)
	sum2+=a[i];*/
	cout<<sum1<<" "<<sum2<<endl;
	return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define pii pair<int, int>
#define VI vector < int >
#define PB push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORD(i,a,b) for(i=a;i>b;i--)
typedef long long LL;
using namespace std;
int main()
{
	int i,j,k,n,t;
	int a[1000000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	int sum1=0,sum2=0;
	for(i=n-1;i>=0;i-=2)
	sum1+=a[i];
	for(i=n-2;i>=0;i-=2)
	sum2+=a[i];
	cout<<sum1<<" "<<sum2<<endl;
	return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define pii pair<int, int>
#define VI vector < int >
#define PB push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORD(i,a,b) for(i=a;i>b;i--)
typedef long long LL;
using namespace std;
int main()
{
	int i,j,k,n,t;
	int a[1000000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	int sum1=0,sum2=0;
	int end=n-1,start=0,c=0;
	while(start<=end)
	{
		int add;
//		cout<<start<<" "<<end<<endl;
		if(start>end)
		{
			start++;
			add=start-1;
		}
		else
		{
			end--;
			add=end+1;
		}
//		cout<<a[add]<<endl;
		if(c%2==0)
			sum1+=a[add];
		else
			sum2+=a[add];
		c++;
	}
/*	for(i=n-1;i>=0;i-=2)
	sum1+=a[i];
	for(i=n-2;i>=0;i-=2)
	sum2+=a[i];*/
	cout<<sum1<<" "<<sum2<<endl;
	return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define pii pair<int, int>
#define VI vector < int >
#define PB push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORD(i,a,b) for(i=a;i>b;i--)
typedef long long LL;
using namespace std;
int main()
{
	int i,j,k,n,t;
	int a[1000000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	int sum1=0,sum2=0;
	int end=n-1,start=0,c=0;
	while(start<=end)
	{
		int add;
//		cout<<start<<" "<<end<<endl;
		if(a[start]>a[end])
		{
			start++;
			add=start-1;
		}
		else
		{
			end--;
			add=end+1;
		}
//		cout<<a[add]<<endl;
		if(c%2==0)
			sum1+=a[add];
		else
			sum2+=a[add];
		c++;
	}
/*	for(i=n-1;i>=0;i-=2)
	sum1+=a[i];
	for(i=n-2;i>=0;i-=2)
	sum2+=a[i];*/
	cout<<sum1<<" "<<sum2<<endl;
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

#define For(i,a,b) for(int i =(a);i<(b);i++)
#define ll long long
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(V) V.begin(),V.end()
/*****************************************************************************/
int a[1010],n;

int main(){
    cin >> n;
    For(i,0,n) {
        cin >> a[i];
    }
    ll ret[2];
    int st = 0,en=n-1;
    For(i,0,n){
        if (a[st]>a[en]){
            ret[i%2]+=a[st++];
        } else{
            ret[i%2]+=a[en--];
        }
    }
    cout << ret[0] << ' '<< ret[1] ;
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

#define For(i,a,b) for(typeof(a) i =(a);i<(b);i++)
#define ll long long
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(V) V.begin(),V.end()
/*****************************************************************************/
int a[1010],n;

int main(){
    scanf("%d",&n);
    For(i,0,n) {
        scanf("%d",&a[i]);
    }
    ll ret[2];
    int st = 0,en=n-1;
    For(i,0,n){
        if (a[st]>a[en]){
            ret[i%2]+=a[st++];
        } else{
            ret[i%2]+=a[en--];
        }
    }
    cout << ret[0] << ' '<< ret[1] << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

#define For(i,a,b) for(int i =(a);i<(b);i++)
#define ll long long
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(V) V.begin(),V.end()
/*****************************************************************************/
int a[1010],n;

int main(){
    cin >> n;
    For(i,0,n) {
        cin >> a[i];
    }
    int ret[2];
    ret[0]=ret[1]=0;
    int st = 0,en=n-1;
    For(i,0,n){
        if (a[st]>a[en]){
            ret[i%2]+=a[st++];
        } else{
            ret[i%2]+=a[en--];
        }
    }
    cout << ret[0] << ' '<< ret[1] << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

#define For(i,a,b) for(int i =(a);i<(b);i++)
#define ll long long
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(V) V.begin(),V.end()
/*****************************************************************************/
int a[1010],n;

int main(){
    scanf("%d",&n);
    For(i,0,n) {
        scanf("%d",&a[i]);
    }
    ll ret[2];
    int st = 0,en=n-1;
    For(i,0,n){
        if (a[st]>a[en]){
            ret[i%2]+=a[st++];
        } else{
            ret[i%2]+=a[en--];
        }
    }
    cout << ret[0] << ' '<< ret[1] ;
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

#define For(i,a,b) for(int i =(a);i<(b);i++)
#define ll long long
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(V) V.begin(),V.end()
/*****************************************************************************/
int a[1010],n;

int main(){
    cin >> n;
    For(i,0,n) {
        cin >> a[i];
    }
    int ret[2];
    int st = 0,en=n-1;
    For(i,0,n){
        if (a[st]>a[en]){
            ret[i%2]+=a[st++];
        } else{
            ret[i%2]+=a[en--];
        }
    }
    cout << ret[0] << ' '<< ret[1] ;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <ctime>
#include <list>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iostream>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>

using namespace std;

#define F_TO_R          freopen("input.txt", "r" , stdin)
#define _pair(x,y)      make_pair(x,y)
#define INF             (1<<30)
#define sf              scanf
#define pf              printf
#define mem(a,b)        memset(a, b, sizeof(a))
#define fs              first
#define se              second
#define MAX             31300
#define MAXR            205
#define MAXC            5
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int u_int;
const double PI = 2.0 * acos(0.0);
const double eps = 1e-11;
const int _mod = 1000000;
const int save[10][4] = {{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},{6,8,4,2},{1,9}};
int todigit(char c) { return ( isdigit(c) ) ?( c - 48 ) : c ; }
int toint(string s) { int r = 0; istringstream sin(s); sin >> r; return r; }
ll tolong(string s) {  ll r = 0; istringstream sin(s); sin >> r; return r; }
double todouble(string s) { double r = 0.0 ; istringstream sin(s); sin >> r; return r; }
bool bit_check(int k , int p){ return (bool)(k&(1 << p)); }
int bit_set(int k , int p) { return k = (k|(1 << p));}
template < class T > string toString(T n) { ostringstream ost; ost << n; ost.flush() ; return ost.str(); }
template < class T > T Abs(T x) { return x > 0 ? x : -x;}
template < class T > T power(T N , T P) { return (P == 0) ?  1 : N * power(N , P - 1); }
template < class T > inline T max_(T a,T b) {return a > b ? a : b ;}
template < class T > inline T min_(T a,T b) {return a < b ? a : b ;}
template < class T > inline T gcd(T a,T b) {if(a < 0) return gcd(-a,b); if(b < 0)return gcd(a,-b); return (b == 0) ? a : gcd(b ,a % b);}
template < class T > inline T lcm(T a,T b) {if(a < 0) return lcm(-a,b); if(b < 0)return lcm(a,-b); return a * (b / gcd(a , b));}

//ll big_mod(ll n , ll  p , ll m)
//{
//  ll sum;
//  if(!p) return 1;
//  if(p % 2 == 0) { sum = big_mod(n  , p / 2 , m); return ((sum % m ) *(sum % m)) % m; }
//  else return (( n % m) * (big_mod( n , p - 1 , m) % m ))% m;
//}

// bool siv[MAX];
// int prime[MAX];
//void is_prime()
//{
//    int k = 0;
//    int n = sqrt(MAX);
//    prime[k++] = 2;
//    for(int i = 3 ; i < n ; i += 2) if(!siv[i]) for(int j = i * i ; j < MAX ; j += (2 * i)) siv[j] = true;
//    for(int i = 3 ; i < MAX ; i += 2) if(!siv[i]) prime[k++] = i;
//}

//int one[]={1,0,-1,0}; //4 Direction
//int two[]={0,1,0,-1}; //4 Direction
//int two[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int one[]={2,1,-1,-2,-2,-1,1,2}; //Knight Direction
//int two[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

///***************************************  END  **********************************************///

int main()
{

    list < int > le;
    int n, temp, one = 0 , two = 0;
    sf("%d", &n);
    while(n--)
    {
        sf("%d", &temp);
        le.push_back(temp);
    }
    while(!le.empty())
    {
        if(le.front() >= le.back())
        {
            one = one + le.front();
            le.pop_front();
        }
        else
        {
            one = one + le.back();
            le.pop_back();
        }
        if(le.empty()) break;
        if(le.front() < le.back())
        {
            two = two + le.back();
            le.pop_back();
        }
        else
        {
            two = two + le.front();
            le.pop_front();
        }
    }
    pf("%d %d\n", one , two);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <ctime>
#include <list>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iostream>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>

using namespace std;

#define F_TO_R          freopen("input.txt", "r" , stdin)
#define _pair(x,y)      make_pair(x,y)
#define INF             (1<<30)
#define sf              scanf
#define pf              printf
#define mem(a,b)        memset(a, b, sizeof(a))
#define fs              first
#define se              second
#define MAX             1010
#define MAXR            50010
#define MAXC            1000000000000LL
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int u_int;
const double PI = 2.0 * acos(0.0);
const double eps = 1e-11;
const int _mod = 1000000;
const int save[10][4] = {{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},{6,8,4,2},{1,9}};
int todigit(char c) { return ( isdigit(c) ) ?( c - 48 ) : c ; }
int toint(string s) { int r = 0; istringstream sin(s); sin >> r; return r; }
ll tolong(string s) {  ll r = 0; istringstream sin(s); sin >> r; return r; }
double todouble(string s) { double r = 0.0 ; istringstream sin(s); sin >> r; return r; }
bool bit_check(int k , int p){ return (bool)(k&(1 << p)); }
int bit_set(int k , int p) { return k = (k|(1 << p));}
template < class T > string toString(T n) { ostringstream ost; ost << n; ost.flush() ; return ost.str(); }
template < class T > T Abs(T x) { return x > 0 ? x : -x;}
template < class T > T power(T N , T P) { return (P == 0) ?  1 : N * power(N , P - 1); }
template < class T > inline T max_(T a,T b) {return a > b ? a : b ;}
template < class T > inline T min_(T a,T b) {return a < b ? a : b ;}
template < class T > inline T gcd(T a,T b) {if(a < 0) return gcd(-a,b); if(b < 0)return gcd(a,-b); return (b == 0) ? a : gcd(b ,a % b);}
template < class T > inline T lcm(T a,T b) {if(a < 0) return lcm(-a,b); if(b < 0)return lcm(a,-b); return a * (b / gcd(a , b));}

//ll big_mod(ll n , ll  p , ll m)
//{
//  ll sum;
//  if(!p) return 1;
//  if(p % 2 == 0) { sum = big_mod(n  , p / 2 , m); return ((sum % m ) *(sum % m)) % m; }
//  else return (( n % m) * (big_mod( n , p - 1 , m) % m ))% m;
//}

//bool siv[MAX];
//int prime[MAXR];
//void is_prime()
//{
//    int k = 0;
//    int n = sqrt(MAX);
//    prime[k++] = 2;
//    for(int i = 3 ; i < n ; i += 2) if(!siv[i]) for(int j = i * i ; j < MAX ; j += (2 * i)) siv[j] = true;
//    for(int i = 3 ; i < MAX ; i += 2) if(!siv[i]) prime[k++] = i;
//}

//int one[]={1,0,-1,0}; //4 Direction
//int two[]={0,1,0,-1}; //4 Direction
//int two[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int one[]={2,1,-1,-2,-2,-1,1,2}; //Knight Direction
//int two[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

///***************************************  END  **********************************************///

list < int > le;

int main()
{
    int t, temp, one = 0 , two = 0;
    sf("%d", &t);
    while(t--)
    {
        sf("%d", &temp);
        le.push_back(temp);
    }
    while(!le.empty())
    {
        if(le.front() >= le.back())
        {
            one = one + le.front();
            le.pop_front();
        }
        if(le.empty()) break;
        else
        {
            two = two + le.back();
            le.pop_back();
        }
    }
    pf("%d %d", two , one);
    return 0;
}