//hail to jwalaji
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli n,q,ans;
vector<lli> arr(300009);
int stat[300009];
void check(lli i){
   lli left,right;
   left=i-1;
   right=i+1;
   if(i==0){
    if(arr[right]<arr[i]){
        stat[i]=1;
        ans+=arr[i];
    }
    else if(arr[right]>arr[i]){
        stat[i]=-1;
    }
    else{
        stat[i]=0;
    }
   }
   else if(i==n-1){
    if(arr[left]<arr[i]){
        stat[i]=1;
        ans+=arr[i];
    }
    else if(arr[left]>arr[i]){
        stat[i]=-1;
    }
    else{
        stat[i]=0;
    }
   }
   else{
    if(arr[left]<arr[i]&&arr[right]<arr[i]){
        stat[i]=1;
        ans+=arr[i];
    }
    else if(arr[left]>arr[i]&&arr[right]>arr[i]){
        stat[i]=-1;
        ans-=arr[i];
    }
    else{
        stat[i]=0;
    }
   }
}
void decheck(lli i){
if(i==0&&stat[i]==1){
    ans-=arr[i];
   // cout<<"cutting : "<< -1*arr[i]<<endl;
}
else if(i==n-1&&stat[i]==1){
    ans-=arr[i];
   // cout<<"cutting : "<< -1*arr[i]<<endl;
}
else if(i>0&&i<n-1){
    if(stat[i]==-1){
        ans+=arr[i];
       // cout<<"cutting : "<< -1*arr[i]<<endl;
    }
    else if(stat[i]==1){
        ans-=arr[i];
       // cout<<"cutting : "<<arr[i]<<endl;
    }
}
}
void makeit(lli l,lli r){
    map<lli,bool> usingit;
if(l-1>=0&&l-1<n){
    if(!usingit[l-1]){
        usingit[l-1]=1;
        decheck(l-1);
    }
}
if(l>=0&&l<n){
    if(!usingit[l]){
        usingit[l]=1;
        decheck(l);
    }
}
if(l+1>=0&&l+1<n){
    if(!usingit[l+1]){
        usingit[l+1]=1;
        decheck(l+1);
    }
}
if(r-1>=0&&r-1<n){
    if(!usingit[r-1]){
        usingit[r-1]=1;
        decheck(r-1);
    }
}
if(r>=0&&r<n){
    if(!usingit[r]){
        usingit[r]=1;
        decheck(r);
    }
}
if(r+1>=0&&r+1<n){
    if(!usingit[r+1]){
        usingit[r+1]=1;
        decheck(r+1);
    }
}
}
void fuckit(lli l,lli r){
    map<lli,bool> usingit;
if(l-1>=0&&l-1<n){
    if(!usingit[l-1]){
        usingit[l-1]=1;
        check(l-1);
    }
}
if(l>=0&&l<n){
    if(!usingit[l]){
        usingit[l]=1;
        check(l);
    }
}
if(l+1>=0&&l+1<n){
    if(!usingit[l+1]){
        usingit[l+1]=1;
        check(l+1);
    }
}
if(r-1>=0&&r-1<n){
    if(!usingit[r-1]){
        usingit[r-1]=1;
        check(r-1);
    }
}
if(r>=0&&r<n){
    if(!usingit[r]){
        usingit[r]=1;
        check(r);
    }
}
if(r+1>=0&&r+1<n){
    if(!usingit[r+1]){
        usingit[r+1]=1;
        check(r+1);
    }
}
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
lli t;
cin>>t;
while(t--){
    cin>>n>>q;
    memset(stat,0,sizeof(stat));
    ans=0;
    for(lli i=0;i<n;i++){
        cin>>arr[i];
    }
    for(lli i=0;i<=n-1;i++){
            check(i);
           //cout<<stat[i]<<" ";
            //cout<<"added : "<<(stat[i])*arr[i]<<endl;
    }
   // cout<<endl;
    cout<<ans<<endl;
    while(q--){
        lli l,r;
        cin>>l>>r;
        l--;r--;
        makeit(l,r);
       // for(lli i=0;i<=n-1;i++){
           // cout<<stat[i]<<" ";
           // cout<<"arr[i] : "<<(stat[i])*arr[i]<<endl;
   // }
    //cout<<"answer after decheck : "<<ans<<endl;
   // cout<<endl<<endl;
        lli temp=arr[l];
        arr[l]=arr[r];
        arr[r]=temp;
        fuckit(l,r);
        //for(lli i=0;i<=n-1;i++){
          //  cout<<stat[i]<<" ";
          //  cout<<"arr[i] : "<<(stat[i])*arr[i]<<endl;
    //}
        cout<<ans<<endl;
}
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        ll n,q;
        cin>>n>>q;
        ll a[n+1]={};
        for(ll i=0;i<n;i++) cin>>a[i];
        vector<ll> ans(q+1,0);
        for(ll i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0);
        }
        ll temp=ans[0];
        ll l,r;
        ll c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;
            temp=ans[c-1];
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(l!=0 &&a[l-1]>a[l]>0) temp-=(a[l-1]-a[l]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            if(r-l>1 && a[r-1]-a[r]>0) temp-=(a[r-1]-a[r]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0);
            temp+=max(a[r]-a[r+1],0);
            if(l!=0 &&a[l-1]>a[l]>0) temp+=(a[l-1]-a[l]);
            if(r-l>1 && a[r-1]-a[r]>0) temp+=(a[r-1]-a[r]);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        int n,q;
        cin>>n>>q;
        int a[n+1]={};
        for(int i=0;i<n;i++) cin>>a[i];
        vector<int> ans(q+1,0);
        for(int i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0);
        }
        int temp=ans[0];
        int l,r;
        int c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0);
            temp+=max(a[r]-a[r+1],0);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        int n,q;
        cin>>n>>q;
        int a[n+1]={};
        for(int i=0;i<n;i++) cin>>a[i];
        vector<int> ans(q+1,0);
        for(int i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0);
        }
        int temp=ans[0];
        int l,r;
        int c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;
            temp=ans[c-1];
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(l!=0 &&a[l-1]>a[l]>0) temp-=(a[l-1]-a[l]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            if(r-l>1 && a[r-1]-a[r]>0) temp-=(a[r-1]-a[r]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0);
            temp+=max(a[r]-a[r+1],0);
            if(l!=0 &&a[l-1]>a[l]>0) temp+=(a[l-1]-a[l]);
            if(r-l>1 && a[r-1]-a[r]>0) temp+=(a[r-1]-a[r]);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        ll n,q;
        cin>>n>>q;
        ll a[n+1]={};
        for(ll i=0;i<n;i++) cin>>a[i];
        vector<ll> ans(q+1,0);
        for(ll i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0LL);
        }
        ll temp=ans[0];
        ll l,r;
        ll c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;
            temp=ans[c-1];
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(l!=0 &&a[l-1]>a[l]>0) temp-=(a[l-1]-a[l]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            if(r-l>1 && a[r-1]-a[r]>0) temp-=(a[r-1]-a[r]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0LL);
            temp+=max(a[r]-a[r+1],0LL);
            if(l!=0 &&a[l-1]>a[l]>0) temp+=(a[l-1]-a[l]);
            if(r-l>1 && a[r-1]-a[r]>0) temp+=(a[r-1]-a[r]);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        ll n,q;
        cin>>n>>q;
        ll a[n+1]={};
        for(ll i=0;i<n;i++) cin>>a[i];
        vector<ll> ans(q+1,0);
        for(ll i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0LL);
        }
        ll temp=ans[0];
        ll l,r;
        ll c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;

            temp=ans[c-1];
            if(r==l) {ans[c]=temp;c++;continue;}
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(l!=0 &&a[l-1]>a[l]>0) temp-=(a[l-1]-a[l]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            if(r-l!=1 && a[r-1]-a[r]>0) temp-=(a[r-1]-a[r]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0LL);
            temp+=max(a[r]-a[r+1],0LL);
            if(l!=0 &&a[l-1]>a[l]>0) temp+=(a[l-1]-a[l]);
            if(r-l!=1 && a[r-1]-a[r]>0) temp+=(a[r-1]-a[r]);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        ll n,q;
        cin>>n>>q;
        ll a[n+1]={};
        for(ll i=0;i<n;i++) cin>>a[i];
        vector<ll> ans(q+1,0);
        for(ll i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0LL);
        }
        ll temp=ans[0];
        ll l,r;
        ll c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;

            temp=ans[c-1];
            if(r==l) continue;
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(l!=0 &&a[l-1]>a[l]>0) temp-=(a[l-1]-a[l]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            if(r-l!=1 && a[r-1]-a[r]>0) temp-=(a[r-1]-a[r]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0LL);
            temp+=max(a[r]-a[r+1],0LL);
            if(l!=0 &&a[l-1]>a[l]>0) temp+=(a[l-1]-a[l]);
            if(r-l!=1 && a[r-1]-a[r]>0) temp+=(a[r-1]-a[r]);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        int n,q;
        cin>>n>>q;
        int a[n+1]={};
        for(int i=0;i<n;i++) cin>>a[i];
        vector<int> ans(q+1,0);
        for(int i=0;i<n;i++)
        {
            ans[0]+=max(a[i]-a[i+1],0);
        }
        int temp=ans[0];
        int l,r;
        int c=1;
        while(c<=q)
        {
            cin>>l>>r;
            l--;
            r--;
            temp=ans[c-1];
            if(a[l]-a[l+1]>0) temp-=(a[l]-a[l+1]);
            if(a[r]-a[r+1]>0) temp-=(a[r]-a[r+1]);
            swap(a[l],a[r]);
            temp+=max(a[l]-a[l+1],0);
            temp+=max(a[r]-a[r+1],0);
            ans[c]=temp;
            c++;

        }
        for(auto&it:ans) cout<<it<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define mod 1000000007
#define mod1 998244353
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pi 3.14159
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<ll> fact(300001);
ll power(ll a,ll n){
    if(n==0)
      return 1;
    if((n%2)==0)
      return (power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1;
    else
      return ((a%mod1)*(power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1)%mod1;
}
ll amount(int index,vector<ll>& a){
    if((index == 0)||(index == a.size()-1))
        return 0;
    if((a[index]>=a[index-1])&&(a[index]>a[index+1])){
        return a[index];
    }
    if((a[index]<=a[index-1])&&(a[index]<a[index+1])){
        return -1*a[index];
    }
    return 0;
}
void solve(){
    ll n,q;
    cin>>n>>q;
    vector<ll> a(n+2);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        if((a[i]>=a[i-1])&&(a[i]>a[i+1])){
            ans+=a[i];
        }
        if((a[i]<=a[i-1])&&(a[i]<a[i+1])){
            ans-=a[i];
        }
    }
    cout<<ans<<"\n";
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        set<int> s;
        s.insert(l),s.insert(l-1),s.insert(l+1),s.insert(r),s.insert(r-1),s.insert(r+1);
        set<int>::iterator it = s.begin();
        while(it!=s.end()){
           ans-=amount(*it,a);
           it++;
        }
        swap(a[l],a[r]);
        it=s.begin();
        while(it!=s.end()){
           ans+=amount(*it,a);
           it++;
        }
        cout<<ans<<"\n";
    }
}
int main(){
    FASTIO
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define mod 1000000007
#define mod1 998244353
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pi 3.14159
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<ll> fact(300001);
ll power(ll a,ll n){
    if(n==0)
      return 1;
    if((n%2)==0)
      return (power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1;
    else
      return ((a%mod1)*(power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1)%mod1;
}
ll amount(int index,vector<ll>& a){
    if((index == 0)||(index == a.size()-1))
        return 0;
    if((a[index]>=a[index-1])&&(a[index]>a[index+1])){
        return a[index];
    }
    if((a[index]<=a[index-1])&&(a[index]<a[index+1])){
        return -1*a[index];
    }
    return 0;
}
void solve(){
    ll n,q;
    cin>>n>>q;
    vector<ll> a(n+2);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        if((a[i]>=a[i-1])&&(a[i]>a[i+1])){
            ans+=a[i];
        }
        if((a[i]<=a[i-1])&&(a[i]<a[i+1])){
            ans-=a[i];
        }
    }
    cout<<ans<<"\n";
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        set<int> s;
        ans-=amount(l,a);
        s.insert(l);
        if(!s.count(l-1)){
         ans-=amount(l-1,a);
         s.insert(l-1);
        }
        if(!s.count(l+1)){
         ans-=amount(l+1,a);
         s.insert(l+1);
        }
        if(!s.count(r)){
         ans-=amount(r,a);
         s.insert(r);
        }
        if(!s.count(r-1)){
         ans-=amount(r-1,a);
         s.insert(r-1);
        }
        if(!s.count(r+1)){
         ans-=amount(r+1,a);
         s.insert(r+1);
        }
        swap(a[l],a[r]);
        s.clear();
        ans+=amount(l,a);
        s.insert(l);
        if(!s.count(l-1)){
         ans+=amount(l-1,a);
         s.insert(l-1);
        }
        if(!s.count(l+1)){
         ans+=amount(l+1,a);
         s.insert(l+1);
        }
        if(!s.count(r)){
         ans+=amount(r,a);
         s.insert(r);
        }
        if(!s.count(r-1)){
         ans+=amount(r-1,a);
         s.insert(r-1);
        }
        if(!s.count(r+1)){
         ans+=amount(r+1,a);
         s.insert(r+1);
        }
        cout<<ans<<"\n";
    }
}
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define mod 1000000007
#define mod1 998244353
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pi 3.14159
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<ll> fact(300001);
ll power(ll a,ll n){
    if(n==0)
      return 1;
    if((n%2)==0)
      return (power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1;
    else
      return ((a%mod1)*(power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1)%mod1;
}
ll amount(int index,vector<ll>& a){
    if((index == 0)||(index == a.size()-1))
        return 0;
    if((a[index]>=a[index-1])&&(a[index]>a[index+1])){
        return a[index];
    }
    if((a[index]<=a[index-1])&&(a[index]<a[index+1])){
        return -1*a[index];
    }
    return 0;
}
void solve(){
    ll n,q;
    cin>>n>>q;
    vector<ll> a(n+2);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        if((a[i]>=a[i-1])&&(a[i]>a[i+1])){
            ans+=a[i];
        }
        if((a[i]<=a[i-1])&&(a[i]<a[i+1])){
            ans-=a[i];
        }
    }
    cout<<ans<<"\n";
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        set<int> s;
        ans-=amount(l,a);
        s.insert(l);
        if(!s.count(l-1)){
         ans-=amount(l-1,a);
         s.insert(l-1);
        }
        if(!s.count(l+1)){
         ans-=amount(l+1,a);
         s.insert(l+1);
        }
        if(!s.count(r)){
         ans-=amount(r,a);
         s.insert(r);
        }
        if(!s.count(r-1)){
         ans-=amount(r-1,a);
         s.insert(r-1);
        }
        if(!s.count(r+1)){
         ans-=amount(r+1,a);
         s.insert(r+1);
        }
        swap(a[l],a[r]);
        s.clear();
        ans+=amount(l,a);
        s.insert(l);
        if(!s.count(l-1)){
         ans+=amount(l-1,a);
         s.insert(l-1);
        }
        if(!s.count(l+1)){
         ans+=amount(l+1,a);
         s.insert(l+1);
        }
        if(!s.count(r)){
         ans+=amount(r,a);
         s.insert(r);
        }
        if(!s.count(r-1)){
         ans+=amount(r-1,a);
         s.insert(r-1);
        }
        if(!s.count(r+1)){
         ans+=amount(r+1,a);
         s.insert(r+1);
        }
        cout<<ans<<"\n";
    }
}
int main(){
    fact[0]=1;
    for(ll i=1;i<=300000;i++){
        fact[i] = ((fact[i-1]%mod1)*i)%mod1;
    }
    int t=1;
    cin>>t;
    for(int i=0;i<t;i++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define mod 1000000007
#define mod1 998244353
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pi 3.14159
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<ll> fact(300001);
ll power(ll a,ll n){
    if(n==0)
      return 1;
    if((n%2)==0)
      return (power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1;
    else
      return ((a%mod1)*(power(((a%mod1)*(a%mod1))%mod1,n/2))%mod1)%mod1;
}
ll amount(int index,vector<ll>& a){
    if((index == 0)||(index == a.size()-1))
        return 0;
    if((a[index]>=a[index-1])&&(a[index]>a[index+1])){
        return a[index];
    }
    if((a[index]<=a[index-1])&&(a[index]<a[index+1])){
        return -1*a[index];
    }
    return 0;
}
void solve(){
    ll n,q;
    cin>>n>>q;
    vector<ll> a(n+2);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        if((a[i]>=a[i-1])&&(a[i]>a[i+1])){
            ans+=a[i];
        }
        if((a[i]<=a[i-1])&&(a[i]<a[i+1])){
            ans-=a[i];
        }
    }
    cout<<ans<<"\n";
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        set<int> s;
        s.insert(l),s.insert(l-1),s.insert(l+1),s.insert(r),s.insert(r-1),s.insert(r+1);
        set<int>::iterator it = s.begin();
        while(it!=s.end()){
           ans-=amount(*it,a);
           it++;
        }
        swap(a[l],a[r]);
        it=s.begin();
        while(it!=s.end()){
           ans+=amount(*it,a);
           it++;
        }
        cout<<ans<<"\n";
    }
}
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        solve();
    }
}


    #include<bits/stdc++.h>
    //#include <boost/multiprecision/cpp_int.hpp>
    //using namespace boost::multiprecision;
    #define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    //#pragma GCC optimize("Ofast")
    #pragma GCC target("avx,avx2,fma")
    //#pragma GCC optimization ("unroll-loops"
    #pragma GCC optimize "trapv"
    #define _GLIBCXX_DEBUG
    #define ll int
    #define ld long double
    #define ull unsigned long long int  // ranges from (0 - twice of long long int)
    #define rep(i,a,n) for (ll i=a;i<n;i++)
    #define per(i,a,n) for (ll i=n-1;i>=a;i--)
    #define pb push_back
    #define mp make_pair
    #define vll vector<ll>
    #define MOD 1000000007LL
    #define llpair pair<ll,ll>
    #define INF 1000000000000000000ll		// this is 1e18 long long
    #define np next_permutation
    #define PI acos(-1)
    #define DEB(x) cout<<#x<<" "<<x<<endl;
    #define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
    #define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
    #define all(x) x.begin(),x.end()
    #define sortall(x) sort(all(x))
    #define clr(x) memset(x,0,sizeof(x))
    using namespace std;
    ll ans,a[100005],n;
    void ins(ll i)
    {
        if(i==0 || i==n+1) return;
        if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
        if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
    }
    void del(ll i)
    {
        if(i==0 || i==n+1) return;
        if(a[i]>a[i+1] && a[i]>a[i-1]) ans-=a[i];
        if(a[i]<a[i+1] && a[i]<a[i-1]) ans+=a[i];
    }
    int main()
    {
        speed;
        ll t=1;
        cin>>t;
        while(t--)
        {
        ll q;
        ans=0;
        cin>>n>>q;
        a[0]=-1;
        a[n+1]=-1;
        rep(i,1,n+1)
        {
            cin>>a[i];
        }
        rep(i,1,n+1)
        {
            if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
            if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
        }
        cout<<ans<<endl;
        while(q--)
        {
            ll l,r;
            cin>>l>>r;
            del(l-1);
            del(l);
            del(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) del(r-1);
                if(r!=l+1) del(r);
                del(r+1);
            }
            swap(a[l],a[r]);
            ins(l-1);
            ins(l);
            ins(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) ins(r-1);
                if(r!=l+1) ins(r);
                ins(r+1);
            }
            cout<<ans<<endl;
        }
        }
        return 0;
    }
    /*
    test cases
    1
    2 3
    6 5 4
    1 2 3
    1 6
    2 5
    3 4

    */


#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops"
#pragma GCC optimize "trapv"
#define _GLIBCXX_DEBUG
#define ll long long int
#define ld long double
#define ull unsigned long long int  // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define vll vector<ll>
#define MOD 1000000007LL
#define llpair pair<ll,ll>
#define INF 1000000000000000000ll		// this is 1e18 long long
#define np next_permutation
#define PI acos(-1)
#define DEB(x) cout<<#x<<" "<<x<<endl;
#define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
#define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
ll ans,a[1000005],n;
void ins(ll i)
{
    if(i==0 || i==n+1) return;
    if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
    if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
}
void del(ll i)
{
    if(i==0 || i==n+1) return;
    if(a[i]>a[i+1] && a[i]>a[i-1]) ans-=a[i];
    if(a[i]<a[i+1] && a[i]<a[i-1]) ans+=a[i];
}
int main()
{
    speed;
    ll t=1;
    cin>>t;
    while(t--)
    {
        ll q;
        ans=0;
        cin>>n>>q;
        a[0]=-1;
        a[n+1]=-1;
        rep(i,1,n+1)
        {
            cin>>a[i];
        }
        rep(i,1,n+1)
        {
            if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
            if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
        }
        cout<<ans<<endl;
        while(q--)
        {
            ll l,r;
            cin>>l>>r;
            del(l-1);
            del(l);
            del(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) del(r-1);
                if(r!=l+1) del(r);
                del(r+1);
            }
            swap(a[l],a[r]);
            ins(l-1);
            ins(l);
            ins(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) ins(r-1);
                if(r!=l+1) ins(r);
                ins(r+1);
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
    /*
    test cases
    1
    2 3
    6 5 4
    1 2 3
    1 6
    2 5
    3 4

    */


    #include<bits/stdc++.h>
    //#include <boost/multiprecision/cpp_int.hpp>
    //using namespace boost::multiprecision;
    #define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    //#pragma GCC optimize("Ofast")
    //#pragma GCC target("avx,avx2,fma")
    //#pragma GCC optimization ("unroll-loops"
    #pragma GCC optimize "trapv"
    #define _GLIBCXX_DEBUG
    #define ll long long int
    #define ld long double
    #define ull unsigned long long int  // ranges from (0 - twice of long long int)
    #define rep(i,a,n) for (ll i=a;i<n;i++)
    #define per(i,a,n) for (ll i=n-1;i>=a;i--)
    #define pb push_back
    #define mp make_pair
    #define vll vector<ll>
    #define MOD 1000000007LL
    #define llpair pair<ll,ll>
    #define INF 1000000000000000000ll		// this is 1e18 long long
    #define np next_permutation
    #define PI acos(-1)
    #define DEB(x) cout<<#x<<" "<<x<<endl;
    #define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
    #define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
    #define all(x) x.begin(),x.end()
    #define sortall(x) sort(all(x))
    #define clr(x) memset(x,0,sizeof(x))
    using namespace std;
    ll ans,a[100005],n,q;
    void ins(ll i)
    {
        if(i==0 || i==n+1) return;
        if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
        if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
    }
    void del(ll i)
    {
        if(i==0 || i==n+1) return;
        if(a[i]>a[i+1] && a[i]>a[i-1]) ans-=a[i];
        if(a[i]<a[i+1] && a[i]<a[i-1]) ans+=a[i];
    }
    int main()
    {
        speed;
        ll t=1;
        cin>>t;
        while(t--)
        {

        ans=0;
        cin>>n>>q;
        a[0]=-1;
        a[n+1]=-1;
        rep(i,1,n+1)
        {
            cin>>a[i];
        }
        rep(i,1,n+1)
        {
            if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
            if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
        }
        cout<<ans<<endl;
        while(q--)
        {
            ll l,r;
            cin>>l>>r;
            del(l-1);
            del(l);
            del(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) del(r-1);
                if(r!=l+1) del(r);
                del(r+1);
            }
            swap(a[l],a[r]);
            ins(l-1);
            ins(l);
            ins(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) ins(r-1);
                if(r!=l+1) ins(r);
                ins(r+1);
            }
            cout<<ans<<endl;
        }
        }
        return 0;
    }
    /*
    test cases
    1
    2 3
    6 5 4
    1 2 3
    1 6
    2 5
    3 4

    */


#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops"
#pragma GCC optimize "trapv"
#define _GLIBCXX_DEBUG
#define ll int
#define ld long double
#define ull unsigned long long int  // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define vll vector<ll>
#define MOD 1000000007LL
#define llpair pair<ll,ll>
#define INF 1000000000000000000ll		// this is 1e18 long long
#define np next_permutation
#define PI acos(-1)
#define DEB(x) cout<<#x<<" "<<x<<endl;
#define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
#define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
ll ans,a[300005],n;
void ins(ll i)
{
    if(i==0 || i==n+1) return;
    if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
    if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
}
void del(ll i)
{
    if(i==0 || i==n+1) return;
    if(a[i]>a[i+1] && a[i]>a[i-1]) ans-=a[i];
    if(a[i]<a[i+1] && a[i]<a[i-1]) ans+=a[i];
}
int main()
{
    speed;
    ll t=1;
    cin>>t;
    while(t--)
    {
        ll q;
        ans=0;
        cin>>n>>q;
        a[0]=-1;
        a[n+1]=-1;
        rep(i,1,n+1)
        {
            cin>>a[i];
        }
        rep(i,1,n+1)
        {
            if(a[i]>a[i+1] && a[i]>a[i-1]) ans+=a[i];
            if(a[i]<a[i+1] && a[i]<a[i-1]) ans-=a[i];
        }
        cout<<ans<<endl;
        while(q--)
        {
            ll l,r;
            cin>>l>>r;
            del(l-1);
            del(l);
            del(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) del(r-1);
                if(r!=l+1) del(r);
                del(r+1);
            }
            swap(a[l],a[r]);
            ins(l-1);
            ins(l);
            ins(l+1);
            if(l!=r)
            {
                if(r!=l+1 && r-1!=l+1) ins(r-1);
                if(r!=l+1) ins(r);
                ins(r+1);
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
    /*
    test cases
    1
    2 3
    6 5 4
    1 2 3
    1 6
    2 5
    3 4

    */


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll  pair <long long,long long>
#define ss second
#define ff first
#define inf (ll)1e15
#define mod 998244353
#define ld long double
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define bug(x) cout << #x <<": " << x << '\n';
const ll N=300005;
ll v[N],ans ;
void add(ll i)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans+=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans-=v[i] ;

}
void sub(ll i)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans-=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans+=v[i] ;
}
int main()
{
	ll t ; cin>>t ;
	while(t--)
	{
		ll n,q ; cin>>n>>q ;

		v[0]=INT_MIN ;
		v[n+1]=INT_MIN ;

		 ans=0 ;
		for(ll i=1;i<=n;i++)
		{
			cin>>v[i] ;

		}
		for(ll i=1;i<=n;i++)
		{
			add(i) ;
		}
		cout<<ans<<endl ;

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

			sub(l) ; sub(r) ;
			if(l-1>=1) sub(l-1) ;
			if(r+1<=n) sub(r+1) ;

			if(l+1<r) sub(l+1) ;

			if(r-1>l&&l+1!=r-1) sub(r-1) ;

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

			add(l) ; add(r) ;

			if(l-1>=1) add(l-1) ;
			if(r+1<=n) add(r+1) ;

			if(l+1<r) add(l+1) ;

			if(r-1>l&&l+1!=r-1) add(r-1) ;

			cout<<ans<<endl ;

		}

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll  pair <long long,long long>
#define ss second
#define ff first
#define inf (ll)1e15
#define mod 998244353
#define ld long double
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define bug(x) cout << #x <<": " << x << '\n';
const ll N=300005;
ll v[N],ans ;
void add(ll i)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans+=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans-=v[i] ;

}
void sub(ll i)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans-=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans+=v[i] ;
}
int main()
{
	fast ;
	ll t ; cin>>t ;
	while(t--)
	{
		ll n,q ; cin>>n>>q ;

		v[0]=INT_MIN ;
		v[n+1]=INT_MIN ;

		 ans=0 ;
		for(ll i=1;i<=n;i++)
		{
			cin>>v[i] ;

		}
		for(ll i=1;i<=n;i++)
		{
			add(i) ;
		}
		cout<<ans<<endl ;

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

			sub(l) ; sub(r) ;
			if(l-1>=1) sub(l-1) ;
			if(r+1<=n) sub(r+1) ;

			if(l+1<r) sub(l+1) ;

			if(r-1>l&&l+1!=r-1) sub(r-1) ;

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

			add(l) ; add(r) ;

			if(l-1>=1) add(l-1) ;
			if(r+1<=n) add(r+1) ;

			if(l+1<r) add(l+1) ;

			if(r-1>l&&l+1!=r-1) add(r-1) ;

			cout<<ans<<endl ;

		}

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll  pair <long long,long long>
#define ss second
#define ff first
#define inf (ll)1e15
#define mod 998244353
#define ld long double
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define bug(x) cout << #x <<": " << x << '\n';
const ll N=300005;
void add(vector<ll>&v,ll i,ll &ans)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans+=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans-=v[i] ;

}
void sub(vector<ll>&v,ll i,ll &ans)
{
	if(v[i]>v[i-1]&&v[i]>v[i+1]) ans-=v[i] ;

	if(v[i]<v[i-1]&&v[i]<v[i+1]) ans+=v[i] ;
}
int main()
{
	ll t ; cin>>t ;
	while(t--)
	{
		ll n,q ; cin>>n>>q ;
		vector<ll>v(n+2) ;

		v[0]=INT_MIN ;
		v[n+1]=INT_MIN ;

		ll ans=0 ;
		for(ll i=1;i<=n;i++)
		{
			cin>>v[i] ;

		}
		for(ll i=1;i<=n;i++)
		{
			add(v,i,ans) ;
		}
		cout<<ans<<endl ;

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

			sub(v,l,ans) ; sub(v,r,ans) ;
			if(l-1>=1) sub(v,l-1,ans) ;
			if(r+1<=n) sub(v,r+1,ans) ;

			if(l+1<r) sub(v,l+1,ans) ;

			if(r-1>l&&l+1!=r-1) sub(v,r-1,ans) ;

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

			add(v,l,ans) ; add(v,r,ans) ;

			if(l-1>=1) add(v,l-1,ans) ;
			if(r+1<=n) add(v,r+1,ans) ;

			if(l+1<r) add(v,l+1,ans) ;

			if(r-1>l&&l+1!=r-1) add(v,r-1,ans) ;

			cout<<ans<<endl ;

		}

	}
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long
#define N 300010
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

ll a[N], ans = 0  ;
void Add( int i ){
    if( a[i-1] < a[i] && a[i+1] < a[i] ) ans += a[i] ;
    else if( a[i-1] > a[i] && a[i+1] > a[i] ) ans -= a[i] ;
}

void Remove( int i ){
    if( a[i-1] > a[i] && a[i+1] > a[i] ) ans += a[i] ;
    else if( a[i-1] < a[i] && a[i+1] < a[i] ) ans -= a[i] ;
}

int main()
{
    ll t, n , q, l, r;
    cin>>t;
    while( t-- ){
        cin>>n>>q ;
        a[0] = a[n+1] = ans = 0 ;
        for( int i = 1 ; i <= n ; i++ ){
            cin>>a[i] ;
        }
        for( int i = 1; i <= n ; i++){
            Add(i);
        }
        cout<<ans<<endl ;

        for( ll j = 0 ; j < q ; j++ ){
            cin>>l>>r;
            set<ll>to_be_changed ;

            for(ll i = -1 ; i <= 1 ; i++ ){
                if( 0 < (l+i) && (l+i) <= n ) to_be_changed.insert(l+i) ;
                if( 0 < (r+i) && (r+i) <= n ) to_be_changed.insert(r+i) ;
            }

            for( ll idx:to_be_changed){
                Remove(idx) ;
            }

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

            for( ll idx:to_be_changed){
                Add(idx) ;
            }

            cout<<ans<<endl ;
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long
#define N 300010
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

ll a[N], ans = 0  ;
void Add( int i ){
    if( a[i-1] < a[i] && a[i+1] < a[i] ) ans += a[i] ;
    else if( a[i-1] > a[i] && a[i+1] > a[i] ) ans -= a[i] ;
}

void Remove( int i ){
    if( a[i-1] > a[i] && a[i+1] > a[i] ) ans += a[i] ;
    else if( a[i-1] < a[i] && a[i+1] < a[i] ) ans -= a[i] ;
}

int main()
{
    ll t, n , q, l, r;
    scanf("%lld", &t);
    while( t-- ){
        cin>>n>>q ;
        a[0] = a[n+1] = ans = 0 ;
        for( int i = 1 ; i <= n ; i++ ){
             scanf("%lld", &a[i]);
        }
        for( int i = 1; i <= n ; i++){
            Add(i);
        }
        printf("%lld\n", ans);

        for( ll j = 0 ; j < q ; j++ ){
            scanf("%lld %lld", &l, &r);
            set<ll>to_be_changed ;

            for(ll i = -1 ; i <= 1 ; i++ ){
                if( 0 < (l+i) && (l+i) <= n ) to_be_changed.insert(l+i) ;
                if( 0 < (r+i) && (r+i) <= n ) to_be_changed.insert(r+i) ;
            }

            for( ll idx:to_be_changed){
                Remove(idx) ;
            }

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

            for( ll idx:to_be_changed){
                Add(idx) ;
            }

            printf("%lld\n", ans);
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define M 1000000007
#define N 300010
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

// Thanks to "Um_nik"
int t, n, q, l, r;
ll ans, arr[N];
void printArray(int*, int);

/*
ADD(int):
    adds the contribution by this index
    if it turns out be a local maxima, we add arr[idx] to the ans
    if it turns out be a local minima, we substract arr[idx] from our answer
*/
void ADD(int idx) {
    if(arr[idx] > arr[idx - 1] & arr[idx] > arr[idx + 1])
        ans += arr[idx];
    if(arr[idx] < arr[idx - 1] & arr[idx] < arr[idx + 1])
        ans -= arr[idx];
}
/*
REMOVE(int):
    substracts the contribution by this index
    if it had been a local maxima, we substract arr[idx] from the ans, as we r now removing this element, removing its earlier contribution
    if it had been a local minima, we add back arr[idx] to the ans, as we r now removing this element, removing its earlier contribution
*/
void REMOVE(int idx) {
    if(arr[idx] > arr[idx - 1] & arr[idx] > arr[idx + 1])
        ans -= arr[idx];
    if(arr[idx] < arr[idx - 1] & arr[idx] < arr[idx + 1])
        ans += arr[idx];
}

int main() {
    scanf("%d", &t);

    while(t--) {
        scanf("%d %d", &n, &q);
        for(int i=1; i<=n; i++) scanf("%lld", &arr[i]);
        arr[0] = arr[n + 1] = ans = 0;

        for(int i=1; i<=n; i++)   // add the contribution from this index, this could be local minima or local maxima
            ADD(i);

        printf("%lld\n", ans);

        for(int i=0; i<q; i++) {
            scanf("%d %d", &l, &r);

            set<int> tobeChanged;               // used set because affected indices can overlap
            for(int i=-1; i<=1; i++) {
                if(0 < (l + i) && (l + i) <= n) tobeChanged.insert(l + i);         // if valid
                if(0 < (r + i) && (r + i) <= n) tobeChanged.insert(r + i);         // if valid
            }

            for(int idx: tobeChanged)         // remove the contribution from these affected indices
                REMOVE(idx);

            swap(arr[l], arr[r]);           // performs swapping

            for(int idx: tobeChanged)       // add back the contribution from these affected indices
                ADD(idx);

            printf("%lld\n", ans);
        }
    }
	return 0;
}

void printArray(int *nums, int n) {
    for(int i=0; i<n; i++) printf("%d ", nums[i]);
    printf("\n");
}


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

int main()
{
  ll t, n, q ;
  cin>>t ;
  while( t-- ){
      cin>>n>>q ;
      vector<ll>a(n+1, 0) ;
      vector<pair<ll, ll>>v(q+1) ;
      v[0].first = 0 ;
      v[0].second = 0 ;

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

    for( ll i = 1 ; i <= q ; i++ ){
        cin>>v[i].first>>v[i].second ;
        v[i].first-- ;
        v[i].second-- ;
    }
    int j = 0 ;
    while( j <= q )
    {
        swap(a[v[j].first] , a[v[j].second]) ; j++ ;
        ll max_i = 0 ;
        ll min_i = 1 ;

        ll ans = 0 ;

        while( max_i < n && min_i < n+1){
            while( max_i < n && a[min_i] > a[max_i] ){
              max_i ++;
              min_i ++;
            }
            while( min_i < n && a[min_i+1] < a[min_i]){
              min_i++;
            }
            if( min_i < n+1 && a[max_i] > a[min_i]){
              ans += (a[max_i] - a[min_i]) ;
              max_i = min_i+1;
              min_i = max_i+1 ;
            }else break ;
        }

        cout<<ans<<endl;
     }
  }

 return 0;

}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long
#define N 300010

ll a[N], ans = 0  ;
void Add( int i ){
    if( a[i-1] < a[i] && a[i+1] < a[i] ) ans += a[i] ;
    else if( a[i-1] > a[i] && a[i+1] > a[i] ) ans -= a[i] ;
}

void Remove( int i ){
    if( a[i-1] > a[i] && a[i+1] > a[i] ) ans += a[i] ;
    else if( a[i-1] < a[i] && a[i+1] < a[i] ) ans -= a[i] ;
}

int main()
{
    ll t, n , q, l, r;
    cin>>t;
    while( t-- ){
        cin>>n>>q ;
        a[0] = a[n+1] = ans = 0 ;
        for( int i = 1 ; i <= n ; i++ ){
            cin>>a[i] ;
        }
        for( int i = 1; i <= n ; i++){
            Add(i);
        }
        cout<<ans<<endl ;

        for( ll j = 0 ; j < q ; j++ ){
            cin>>l>>r;
            set<ll>to_be_changed ;

            for(ll i = -1 ; i <= 1 ; i++ ){
                if( 0 < (l+i) && (l+i) <= n ) to_be_changed.insert(l+i) ;
                if( 0 < (r+i) && (r+i) <= n ) to_be_changed.insert(r+i) ;
            }

            for( ll idx:to_be_changed){
                Remove(idx) ;
            }

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

            for( ll idx:to_be_changed){
                Add(idx) ;
            }

            cout<<ans<<endl ;
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    fast;
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[300004];
        a[0]=0;
        a[n+1]=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int ans=0;
        for(int i=0;i<n;i++)
            ans+=max(0ll,a[i+1]-a[i]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            if(l==r)
            {
                cout<<ans<<endl;
                continue;
            }
            int sum1=0,sum2=0;
            sum1+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum1+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum1-=max(0ll,a[l+1]-a[l]);
            swap(a[l],a[r]);
            sum2+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum2+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum2-=max(0ll,a[l+1]-a[l]);
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}
/*
static void change(int ind, int newVal, int[] a) {
        ans-=a[0];
        if (ind>0) ans-=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans-=Math.max(0, a[ind+1]-a[ind]);

        a[ind]=newVal;

        if (ind>0) ans+=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans+=Math.max(0, a[ind+1]-a[ind]);
        ans+=a[0];
*/


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[300004];
        a[0]=0;
        a[n+1]=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int ans=0;
        for(int i=0;i<n;i++)
            ans+=max(0ll,a[i+1]-a[i]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            if(l==r)
            {
                cout<<ans<<endl;
                continue;
            }
            int sum1=0,sum2=0;
            sum1+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum1+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum1-=max(0ll,a[l+1]-a[l]);
            swap(a[l],a[r]);
            sum2+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum2+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum2-=max(0ll,a[l+1]-a[l]);
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}
/*
static void change(int ind, int newVal, int[] a) {
        ans-=a[0];
        if (ind>0) ans-=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans-=Math.max(0, a[ind+1]-a[ind]);

        a[ind]=newVal;

        if (ind>0) ans+=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans+=Math.max(0, a[ind+1]-a[ind]);
        ans+=a[0];
*/


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[30000];
        for(int i=0;i<n;i++)
            cin>>a[i];
        int ans=a[0];
        for(int i=1;i<n;i++)
            ans+=max(0ll,a[i]-a[i-1]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            l--,r--;
            // if(l==r)
            // {
            //     cout<<ans<<endl;
            //     continue;
            // }
            int sum1=0,sum2=0;
            sum1+=max(0ll,l==0?a[l]:a[l]-a[l-1])+max(0ll,l==0 || l==n-1?0:a[l+1]-a[l]);
            //cout<<sum1<<endl;
            sum1+=max(0ll,r==0?a[r]:a[r]-a[r-1])+max(0ll,r==0 || r==n-1?0:a[r+1]-a[r]);
            if(r==l+1 && l)
                sum1-=max(0ll,a[l+1]-a[l]);
            //cout<<"sum1: "<<sum1<<endl;
            swap(a[l],a[r]);
            sum2+=max(0ll,l==0?a[l]:a[l]-a[l-1])+max(0ll,l==0 || l==n-1?0:a[l+1]-a[l]);
            //cout<<sum2<<endl;
            sum2+=max(0ll,r==0?a[r]:a[r]-a[r-1])+max(0ll,r==0 || r==n-1?0:a[r+1]-a[r]);
            if(r==l+1 && l)
                sum2-=max(0ll,a[l+1]-a[l]);
            //cout<<"sum2: "<<sum2<<endl;
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[300001];
        a[0]=0;
        a[n+1]=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int ans=0;
        for(int i=0;i<n;i++)
            ans+=max(0ll,a[i+1]-a[i]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            if(l==r)
            {
                cout<<ans<<endl;
                continue;
            }
            int sum1=0,sum2=0;
            sum1+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum1+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum1-=max(0ll,a[l+1]-a[l]);
            swap(a[l],a[r]);
            sum2+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum2+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum2-=max(0ll,a[l+1]-a[l]);
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}
/*
static void change(int ind, int newVal, int[] a) {
        ans-=a[0];
        if (ind>0) ans-=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans-=Math.max(0, a[ind+1]-a[ind]);

        a[ind]=newVal;

        if (ind>0) ans+=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans+=Math.max(0, a[ind+1]-a[ind]);
        ans+=a[0];
*/


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[30000];
        for(int i=0;i<n;i++)
            cin>>a[i];
        int ans=a[0];
        for(int i=1;i<n;i++)
            ans+=max(0ll,a[i]-a[i-1]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            l--,r--;
            if(l==r)
            {
                cout<<ans<<endl;
                continue;
            }
            int sum1=0,sum2=0;
            sum1+=max(0ll,l==0?a[l]:a[l]-a[l-1])+max(0ll,l==0 || l==n-1?0:a[l+1]-a[l]);
            //cout<<sum1<<endl;
            sum1+=max(0ll,r==0?a[r]:a[r]-a[r-1])+max(0ll,r==0 || r==n-1?0:a[r+1]-a[r]);
            if(r==l+1 && l)
                sum1-=max(0ll,a[l+1]-a[l]);
            //cout<<"sum1: "<<sum1<<endl;
            swap(a[l],a[r]);
            sum2+=max(0ll,l==0?a[l]:a[l]-a[l-1])+max(0ll,l==0 || l==n-1?0:a[l+1]-a[l]);
            //cout<<sum2<<endl;
            sum2+=max(0ll,r==0?a[r]:a[r]-a[r-1])+max(0ll,r==0 || r==n-1?0:a[r+1]-a[r]);
            if(r==l+1 && l)
                sum2-=max(0ll,a[l+1]-a[l]);
            //cout<<"sum2: "<<sum2<<endl;
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mod 1000000007
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define setpres(x) fixed<<setprecision(x)
using namespace std;
int32_t main()
{
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n,q;
        cin>>n>>q;
        int a[30001];
        a[0]=0;
        a[n+1]=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int ans=0;
        for(int i=0;i<n;i++)
            ans+=max(0ll,a[i+1]-a[i]);
        cout<<ans<<endl;
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            if(l==r)
            {
                cout<<ans<<endl;
                continue;
            }
            int sum1=0,sum2=0;
            sum1+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum1+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum1-=max(0ll,a[l+1]-a[l]);
            swap(a[l],a[r]);
            sum2+=max(0ll,a[l]-a[l-1])+max(0ll,a[l+1]-a[l]);
            sum2+=max(0ll,a[r]-a[r-1])+max(0ll,a[r+1]-a[r]);
            if(l+1==r)
                sum2-=max(0ll,a[l+1]-a[l]);
            ans=ans-sum1+sum2;
            cout<<ans<<endl;
        }
    }
}
/*
static void change(int ind, int newVal, int[] a) {
        ans-=a[0];
        if (ind>0) ans-=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans-=Math.max(0, a[ind+1]-a[ind]);

        a[ind]=newVal;

        if (ind>0) ans+=Math.max(0, a[ind]-a[ind-1]);
        if (ind+1<a.length) ans+=Math.max(0, a[ind+1]-a[ind]);
        ans+=a[0];
*/


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

ll arr[1000000];ll n,q;
ll ans;
inline void erase1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1])
	ans-=arr[x];
	if(arr[x]<arr[x+1] && arr[x]<arr[x-1])
	ans += arr[x];

}
inline void insert1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1]){

	//cout<<"insert "<<x<<" "<<arr[x]<<" ";
	ans += arr[x];
	//	cout<<ans<<endl;
	}
	if(arr[x]<arr[x-1] && arr[x]<arr[x+1])
	ans -= arr[x];
}
int main() {

	ll t;
	cin>>t;
	while(t--)
	{

		cin>>n>>q;
		for(ll i=1;i<=n;i++)
		cin>>arr[i];
		arr[0]=-1;
		arr[n+1]=-1;

		ans=0;
		for(ll i=1;i<=n;i++){
		if(arr[i]>arr[i+1] && arr[i]>arr[i-1])
		ans += arr[i];
		else if(arr[i]<arr[i+1] && arr[i]<arr[i-1])
		ans-=arr[i];}

		cout<<ans<<endl;

		ll l,r;
		while(q--)
		{
			cin>>l>>r;
			erase1(l);
			erase1(l+1);
			erase1(l-1);

			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) erase1(r-1);
				if(r!=l+1) erase1(r);
				erase1(r+1);
			}

			swap(arr[l],arr[r]);
			// 	for(ll i=0;i<=n+1;i++)
			// cout<<arr[i]<<" ";
			// cout<<endl;
			// 	cout<<ans<<endl;
			insert1(l);
			insert1(l+1);
			insert1(l-1);
			//	cout<<ans<<endl;
			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) insert1(r-1);
				if(r!=l+1) insert1(r);
				insert1(r+1);
			}
			//	cout<<ans<<endl;
			cout<<ans<<endl;
		}
	}

	return 0;
}


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

ll arr[1000000];ll n,q;
ll ans;
inline void erase1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1])
	ans-=arr[x];
	if(arr[x]<arr[x+1] && arr[x]<arr[x-1])
	ans += arr[x];

}
inline void insert1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1]){

	//cout<<"insert "<<x<<" "<<arr[x]<<" ";
	ans += arr[x];
	//	cout<<ans<<endl;
	}
	if(arr[x]<arr[x-1] && arr[x]<arr[x+1])
	ans -= arr[x];
}
int main() {

	ll t;
	cin>>t;
	while(t--)
	{

		cin>>n>>q;
		for(ll i=1;i<=n;i++)
		scanf("%lld",arr+i);
		arr[0]=-1;
		arr[n+1]=-1;

		ans=0;
		for(ll i=1;i<=n;i++){
		if(arr[i]>arr[i+1] && arr[i]>arr[i-1])
		ans += arr[i];
		else if(arr[i]<arr[i+1] && arr[i]<arr[i-1])
		ans-=arr[i];}

		printf("%lld\n",ans);

		ll l,r;
		while(q--)
		{
			scanf("%lld %lld",&l,&r);
			erase1(l);
			erase1(l+1);
			erase1(l-1);

			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) erase1(r-1);
				if(r!=l+1) erase1(r);
				erase1(r+1);
			}

			swap(arr[l],arr[r]);
			// 	for(ll i=0;i<=n+1;i++)
			// cout<<arr[i]<<" ";
			// cout<<endl;
			// 	cout<<ans<<endl;
			insert1(l);
			insert1(l+1);
			insert1(l-1);
			//	cout<<ans<<endl;
			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) insert1(r-1);
				if(r!=l+1) insert1(r);
				insert1(r+1);
			}
			//	cout<<ans<<endl;
			printf("%lld\n",ans);
		}
	}

	return 0;
}


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

ll arr[1000000];ll n,q;
ll ans;
void erase1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1])
	ans-=arr[x];
	if(arr[x]<arr[x+1] && arr[x]<arr[x-1])
	ans += arr[x];

}
void insert1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1]){

	//cout<<"insert "<<x<<" "<<arr[x]<<" ";
	ans += arr[x];
	//	cout<<ans<<endl;
	}
	if(arr[x]<arr[x-1] && arr[x]<arr[x+1])
	ans -= arr[x];
}
int main() {

	ll t;
	cin>>t;
	while(t--)
	{

		cin>>n>>q;
		for(ll i=1;i<=n;i++)
		cin>>arr[i];
		arr[0]=-1;
		arr[n+1]=-1;

		ans=0;
		for(ll i=1;i<=n;i++){
		if(arr[i]>arr[i+1] && arr[i]>arr[i-1])
		ans += arr[i];
		else if(arr[i]<arr[i+1] && arr[i]<arr[i-1])
		ans-=arr[i];}

		cout<<ans<<endl;

		ll l,r;
		while(q--)
		{
			cin>>l>>r;
			erase1(l);
			erase1(l+1);
			erase1(l-1);

			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) erase1(r-1);
				if(r!=l+1) erase1(r);
				erase1(r+1);
			}

			swap(arr[l],arr[r]);
			// 	for(ll i=0;i<=n+1;i++)
			// cout<<arr[i]<<" ";
			// cout<<endl;
			// 	cout<<ans<<endl;
			insert1(l);
			insert1(l+1);
			insert1(l-1);
			//	cout<<ans<<endl;
			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) insert1(r-1);
				if(r!=l+1) insert1(r);
				insert1(r+1);
			}
			//	cout<<ans<<endl;
			cout<<ans<<endl;
		}
	}

	return 0;
}


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

ll arr[100000];ll n,q;
ll ans;
void erase1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1])
	ans-=arr[x];
	if(arr[x]<arr[x+1] && arr[x]<arr[x-1])
	ans += arr[x];

}
void insert1(ll x)
{
	if(x==0 || x== n+1)
	return ;
	if(arr[x]>arr[x+1] && arr[x]>arr[x-1]){

	//cout<<"insert "<<x<<" "<<arr[x]<<" ";
	ans += arr[x];
	//	cout<<ans<<endl;
	}
	if(arr[x]<arr[x-1] && arr[x]<arr[x+1])
	ans -= arr[x];
}
int main() {

	ll t;
	cin>>t;
	while(t--)
	{

		cin>>n>>q;
		for(ll i=1;i<=n;i++)
		cin>>arr[i];
		arr[0]=-1;
		arr[n+1]=-1;

		ans=0;
		for(ll i=1;i<=n;i++){
		if(arr[i]>arr[i+1] && arr[i]>arr[i-1])
		ans += arr[i];
		else if(arr[i]<arr[i+1] && arr[i]<arr[i-1])
		ans-=arr[i];}

		cout<<ans<<endl;

		ll l,r;
		while(q--)
		{
			cin>>l>>r;
			erase1(l);
			erase1(l+1);
			erase1(l-1);

			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) erase1(r-1);
				if(r!=l+1) erase1(r);
				erase1(r+1);
			}

			swap(arr[l],arr[r]);
			// 	for(ll i=0;i<=n+1;i++)
			// cout<<arr[i]<<" ";
			// cout<<endl;
			// 	cout<<ans<<endl;
			insert1(l);
			insert1(l+1);
			insert1(l-1);
			//	cout<<ans<<endl;
			if(l!=r)
			{
				if(l+1!=r && r-1!=l+1) insert1(r-1);
				if(r!=l+1) insert1(r);
				insert1(r+1);
			}
			//	cout<<ans<<endl;
			cout<<ans<<endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

inline void add(int i, const vector<long long>& v, long long &ans) {
  if (i < 1 || i > v.size()) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

inline void erase(int i, const vector<long long>& v, long long &ans) {
  if (i < 1 || i > v.size()) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans -= v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans += v[i];
}

inline void callAddOrErase(int l, int r, const vector<long long>& v, long long &ans, void f(int, const vector<long long>&, long long& )) {
      f(l-1, v, ans);
      f(l, v, ans);
      f(l+1, v, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, v, ans);
        if (r != (l+1) ) f(r, v, ans);
        f(r+1, v, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int n, q, k, r;
    cin >> n >> q;
    vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      callAddOrErase(k, r, v, ans, erase);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      callAddOrErase(k, r, v, ans, add);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

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

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

inline void callAddOrErase(int l, int r, int &ans, void f(int, int& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int q, l, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    ans = 0;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> l >> r;
      erase(l-1);
      erase(l);
      erase(l+1);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) erase(r-1);
        if (r != (l+1) ) erase(r);
        erase(r+1);
      }

      int temp;
      temp = v[l];
      v[l] = v[r];
      v[r] = temp;
      add(l-1);
      add(l);
      add(l+1);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) add(r-1);
        if (r != (l+1) ) add(r);
        add(r+1);
      }

      printf("%d\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

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

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

inline void callAddOrErase(int l, int r, int &ans, void f(int, int& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    int q, l, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    ans = 0;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> l >> r;
      erase(l-1);
      erase(l);
      erase(l+1);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) erase(r-1);
        if (r != (l+1) ) erase(r);
        erase(r+1);
      }

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

      printf("%d\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

inline void addOrErase(int i, const vector<long long>& v, long long &ans, bool erase) {
  if (i < 1 || i > v.size()) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) if(erase) ans -= v[i]; else ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) if(erase) ans += v[i]; else ans -= v[i];
}

inline void callAddOrErase(int l, int r, const vector<long long>& v, long long &ans, bool erase = false) {
      addOrErase(l-1, v, ans, erase);
      addOrErase(l, v, ans, erase);
      addOrErase(l+1, v, ans, erase);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) addOrErase(r-1, v, ans, erase);
        if (r != (l+1) ) addOrErase(r, v, ans, erase);
        addOrErase(r+1, v, ans, erase);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int n, q, k, r;
    cin >> n >> q;
    vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      callAddOrErase(k, r, v, ans, true);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      callAddOrErase(k, r, v, ans);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

long long v[100000 + 2];
int n;
inline void add(int i, long long &ans) {
  if (i < 1 || i > n) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

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

inline void callAddOrErase(int l, int r, long long &ans, void f(int, long long& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int q, k, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      callAddOrErase(k, r, ans, erase);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      callAddOrErase(k, r, ans, add);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

long long v[300000 + 2];
int n;
inline void add(int i, long long &ans) {
  if (i < 1 || i > n) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

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

inline void callAddOrErase(int l, int r, long long &ans, void f(int, long long& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int q, k, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      callAddOrErase(k, r, ans, erase);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      callAddOrErase(k, r, ans, add);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

long long v[300000 + 2];
int n;
inline void add(int i, long long &ans) {
  if (i < 1 || i > n) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

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

inline void callAddOrErase(int l, int r, long long &ans, void f(int, long long& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int q, l, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> l >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      erase(l-1, ans);
      erase(l, ans);
      erase(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) erase(r-1, ans);
        if (r != (l+1) ) erase(r, ans);
        erase(r+1, ans);
      }

      long long temp;
      temp = v[l];
      v[l] = v[r];
      v[r] = temp;
      add(l-1, ans);
      add(l, ans);
      add(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) add(r-1, ans);
        if (r != (l+1) ) add(r, ans);
        add(r+1, ans);
      }

      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

long long v[300000 + 2];
long long ans = 0;
int n;
inline void add(int i) {
  if (i < 1 || i > n) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

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

inline void callAddOrErase(int l, int r, int &ans, void f(int, int& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    int q, l, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    ans = 0;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> l >> r;
      erase(l-1);
      erase(l);
      erase(l+1);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) erase(r-1);
        if (r != (l+1) ) erase(r);
        erase(r+1);
      }

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

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


#include <bits/stdc++.h>
#include <array>

using namespace std;

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

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

inline void callAddOrErase(int l, int r, int &ans, void f(int, int& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    int q, l, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    ans = 0;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> l >> r;
      erase(l-1);
      erase(l);
      erase(l+1);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) erase(r-1);
        if (r != (l+1) ) erase(r);
        erase(r+1);
      }

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

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


#include <bits/stdc++.h>
#include <array>

using namespace std;

long long v[100000 + 1];
int n;
inline void add(int i, long long &ans) {
  if (i < 1 || i > n) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) ans -= v[i];
}

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

inline void callAddOrErase(int l, int r, long long &ans, void f(int, long long& )) {
      f(l-1, ans);
      f(l, ans);
      f(l+1, ans);
      if (l != r) {
        if ((r-1 != l) && (r-1 != l + 1)) f(r-1, ans);
        if (r != (l+1) ) f(r, ans);
        f(r+1, ans);
      }
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int q, k, r;
    cin >> n >> q;
    // vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      // int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      // set<int> sw(s, s+6);
      callAddOrErase(k, r, ans, erase);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      callAddOrErase(k, r, ans, add);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <array>

using namespace std;

void addOrErase(int i, const vector<long long>& v, long long &ans, bool erase = false) {
  if (i < 1 || i > v.size()) return;
  if ( (v[i-1] <  v[i]) && (v[i] > v[i+1])) if(erase) ans -= v[i]; else ans += v[i];
  if ((v[i-1] > v[i]) && (v[i] < v[i+1])) if(erase) ans += v[i]; else ans -= v[i];
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    int n, q, k, r;
    cin >> n >> q;
    vector<long long> v(n+2);
    v[0] = -1;
    for(int i = 1; i<=n; i++) cin >> v[i];
    v[n+1] = -1;
    // positive is 0, negative is 1
    // vector<array<int, 2> > dp(n, { 0, 0 } );
    long long ans = 0;
    for(int k=1;k<=n;k++) {
      // if local maxima add, if local minima subtract
      if ((v[k-1] > v[k]) && (v[k] < v[k + 1])) ans -= v[k];
      if ((v[k-1] < v[k]) && (v[k] > v[k + 1])) ans += v[k];
    }
    cout << ans << "\n";
    while(q--) {
      cin >> k >> r;
      int s[6] = {k-1, k, k+1, r-1, r, r+1 };
      set<int> sw(s, s+6);
      for(auto a: sw) addOrErase(a, v, ans, true);
      long long temp;
      temp = v[k];
      v[k] = v[r];
      v[r] = temp;
      for(auto a: sw) addOrErase(a, v, ans);
      printf("%lld\n", ans);
    }
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
int a[300001];

void swap(int i,int j)
{
	int temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
}
int Get(int i,int n)
{
	if(i<0 || i>n-1)
	  return 0;

	int cur=a[i];
	int nxt=(i==n-1?0:a[i+1]);
	return max(0,cur-nxt);
}
int main()
{
	int t,n,q;

	cin>>t;

	while(t--)
	{
		cin>>n>>q;

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

		int ans=a[0];

		for(int i=1;i<n;i++)
		{
		   ans+=max(0,a[i]-a[i-1]);
		}
	//	cout<<" Ans is "<<ans<<endl;
		int l,r;
		for(int qq=1;qq<=q;qq++)
		{
		    cin>>l>>r;
			l--;
			r--;

			ans-=Get(l-1,n);
		    ans-=Get(l,n);
		    if(r!=l+1)
		    ans-=Get(r-1,n);
		    ans-=Get(r,n);
		    swap(l,r);
		    ans+=Get(l-1,n);
		    ans+=Get(l,n);
		    if(r!=l+1)
		    ans+=Get(r-1,n);
		    ans+=Get(r,n);

			cout<<ans<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
int a[300001];

void swap(int i,int j)
{
	int temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
}
int Get(int i,int n)
{
	if(i<0 || i>n-1)
	  return 0;

	int cur=a[i];
	int nxt=(i==n-1?0:a[i+1]);
	return max(0,cur-nxt);
}
int main()
{
	int t,n,q;

	cin>>t;

	while(t--)
	{
		cin>>n>>q;

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

		int ans=a[0];

		for(int i=1;i<n;i++)
		{
		   ans+=max(0,a[i]-a[i-1]);
		}
		cout<<ans<<endl;
	//	cout<<" Ans is "<<ans<<endl;
		int l,r;
		for(int qq=1;qq<=q;qq++)
		{
		    cin>>l>>r;
			l--;
			r--;

			ans-=Get(l-1,n);
		    ans-=Get(l,n);
		    if(r!=l+1)
		    ans-=Get(r-1,n);
		    ans-=Get(r,n);
		    swap(l,r);
		    ans+=Get(l-1,n);
		    ans+=Get(l,n);
		    if(r!=l+1)
		    ans+=Get(r-1,n);
		    ans+=Get(r,n);

			cout<<ans<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
int a[300001];

void swap(int i,int j)
{
	int temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
}
int Get(int i,int n)
{
	if(i<0 || i>n-1)
	  return 0;

	int cur=a[i];
	int nxt=(i==n-1?0:a[i+1]);
	return max(0,cur-nxt);
}
int main()
{
	int t,n,q;

	cin>>t;

	while(t--)
	{
		cin>>n>>q;

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

		long long ans=a[0];

		for(int i=1;i<n;i++)
		{
		   ans+=max(0,a[i]-a[i-1]);
		}
		cout<<ans<<endl;
	//	cout<<" Ans is "<<ans<<endl;
		int l,r;
		for(int qq=1;qq<=q;qq++)
		{
		    cin>>l>>r;
			l--;
			r--;

			ans-=Get(l-1,n);
		    ans-=Get(l,n);
		    if(r!=l+1)
		    ans-=Get(r-1,n);
		    ans-=Get(r,n);
		    swap(l,r);
		    ans+=Get(l-1,n);
		    ans+=Get(l,n);
		    if(r!=l+1)
		    ans+=Get(r-1,n);
		    ans+=Get(r,n);

			cout<<ans<<endl;
		}

	}

	return 0;
}


// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
const int maxm = 100001;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
//NOT FOR NEGATIVE NUMBERS
// void sc(int &number)
// {
//     register long long c;
//     number = 0;
//     c = getchar();
//     for (; (c>47 && c<58); c=getchar())
//         number = number *10 + c - 48;

// }

// void dis(vi &a)
// {
//     cout << "\n....Display starts...\n";
//     loop(i, 0, a.size())
//     {
//         cout << a[i] << " ";
//     }
//     cout << "\n....Display ends...\n";
// }

// ll fe(ll base, ll exp) {
//     ll res=1;
//     while(exp>0) {
//       if(exp%2==1) res=(res*base)%MOD;
//       base=(base*base)%MOD;
//       exp/=2;
//     }
//     return res%MOD;
// }

void buildGraph(int n)
{
    adj = vector<vi>(n + 1);
    h = vector<int>(n + 1);
    sz = vector<int>(n + 1);
    loop(i, 0, n - 1)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
}
ll add(ll a, ll b)
{
    return ((a % m) + (b % m)) % m;
}
ll mul(ll a, ll b)
{
    return ((a % m) * (b % m)) % m;
}
ll sub(ll a, ll b)
{
    return ((a % m) - (b % m) + m) % m;
}
vector<ll>in;
ll ans;
int n,q;
int maxv,minv;
void ini(){
   loop1(i,1,n){
       ans += in[i];
       int j = i+1;
       for(;j<=n;j++){
           if(in[j-1]<in[j])
           break;
       }
       ans -= in[j-1];
       i = j-1;
   }
   cout<<ans+in[n]<<"\n";
}

void del(int i){
   //elem only a single one
   ///do nothing
   //elem in min
   if(in[i-1]>in[i] && (!in[i+1] || in[i]<in[i+1])){
       ans += in[i]-in[i-1];
   }
   //elem is max
   if(in[i-1]<in[i] && in[i]>in[i+1]){
       ans -= (in[i]-in[i+1]);
   }

   //elem is mid
   if(in[i-1]>in[i] && in[i]>in[i+1]){
      ans -= in[i-1];
      ans += in[i+1];
   }
   in[i] = 0;
}
void ins(int i,ll val){
    in[i] = val;

    //elem is min
    if(in[i-1]>in[i] && in[i]<in[i+1])
    {
        ans -= (in[i] - in[i - 1]);
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i+1] && in[i] > in[i + 1])
    {
        ans += in[i] - in[i + 1];
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans += in[i - 1];
        ans -= in[i + 1];
    }
}
void solve()
{
    cin>>n>>q;
    in.assign(n+2,0);
    minv = 0;
    maxv = n+1;
    ans=minv;
    in[n+1]=maxv;
    loop1(i,1,n){
        cin>>in[i];
    }
    ini();
    loop(i,0,q){
       int a,b;cin>>a>>b;
       ll av = in[a],bv = in[b];
       del(a);
       del(b);
       ins(a,bv);
       ins(b,av);
       cout<<ans+in[n]<<"\n";
    }
}
int main()
{
    io;

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int test;
    cin >> test;
    // test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}


// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
const int maxm = 100001;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
//NOT FOR NEGATIVE NUMBERS
// void sc(int &number)
// {
//     register long long c;
//     number = 0;
//     c = getchar();
//     for (; (c>47 && c<58); c=getchar())
//         number = number *10 + c - 48;

// }

void dis(vector<ll> &a)
{

    loop(i, 1, a.size())
    {
        cout << a[i] << " ";
    }
   cout<<"\n";
}

vector<ll>in;
ll ans;
int n,q;
int maxv,minv;
ll ini(){
   ll a = 0;
   loop1(i,1,n){
       a += in[i];
       int j = i+1;
       for(;j<=n;j++){
           if(in[j-1]<in[j])
           break;
       }
       a -= in[j-1];
       i = j-1;
   }
   return a;
}

void del(int i)
{
    //elem only a single one
    ///do nothing
    //elem in min
    if (in[i - 1] > in[i] && (!in[i + 1] || in[i] < in[i + 1]))
    {
        ans += in[i] - in[i - 1];
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i] > in[i + 1])
    {
        ans -= (in[i] - in[i + 1]);
    }

    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans -= in[i - 1];
        ans += in[i + 1];
    }
    in[i] = 0;
}
void ins(int i, ll val)
{
    in[i] = val;

    //elem is min
    if (in[i - 1] > in[i] && in[i] < in[i + 1])
    {
        ans -= (in[i] - in[i - 1]);
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i + 1] && in[i] > in[i + 1])
    {
        ans += in[i] - in[i + 1];
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans += in[i - 1];
        ans -= in[i + 1];
    }
}

void solve()
{
    cin>>n>>q;
    in.assign(n+2,0);
    minv = 0;
    maxv = n+1;
    ans=minv;
    in[n+1]=maxv;
    loop1(i,1,n){
        cin>>in[i];
    }
    ans = ini();
    cout<<ans+in[n]<<"\n";
    loop(i,0,q){
       int a,b;cin>>a>>b;
       ll av = in[a],bv = in[b];

        //  swap(in[a],in[b]);
        //  ll val = ini();
        //  swap(in[a],in[b]);
       if(a!=b){
           del(a);
           del(b);
           ins(a, bv);
           ins(b, av);
       }
    //    if(val != ans){
    //      dis(in);
    //    }
       cout<<ans+in[n]<<"\n";
    }
}
int main()
{

    io;

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int test;
    cin >> test;
    // test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}


// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
const int maxm = 100001;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
//NOT FOR NEGATIVE NUMBERS
// void sc(int &number)
// {
//     register long long c;
//     number = 0;
//     c = getchar();
//     for (; (c>47 && c<58); c=getchar())
//         number = number *10 + c - 48;

// }

void dis(vector<ll> &a)
{

    loop(i, 1, a.size() - 1)
    {
        cout << a[i] << " ";
    }
    cout << "\n";
}

vector<ll> in;
ll ans;
int n, q;
int maxv, minv;
ll ini()
{
    ll a = 0;
    loop1(i, 1, n)
    {
        a += in[i];
        int j = i + 1;
        for (; j <= n; j++)
        {
            if (in[j - 1] < in[j])
                break;
        }
        a -= in[j - 1];
        i = j - 1;
    }
    return a;
}

void del(int i)
{
    //elem only a single one
    ///do nothing
    //elem is min
    if (in[i - 1] > in[i] && in[i] < in[i + 1])
    {
        ans += in[i] - in[i - 1];
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i] > in[i + 1])
    {
        ans -= (in[i] - in[i + 1]);
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans -= in[i - 1];
        ans += in[i + 1];
    }
}
void ins(int i, ll val)
{
    in[i] = val;
    //elem is min
    if (in[i - 1] > in[i] && in[i] < in[i + 1])
    {
        ans -= (in[i] - in[i - 1]);
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i] > in[i + 1])
    {
        ans += in[i] - in[i + 1];
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans += in[i - 1];
        ans -= in[i + 1];
    }
}

int single(int i)
{
    return in[i - 1] < in[i] && in[i] < in[i + 1];
}
void solve()
{
    cin >> n >> q;
    in.assign(n + 2, 0);
    minv = 0;
    maxv = n + 1;
    ans = minv;
    in[n + 1] = maxv;
    loop1(i, 1, n)
    {
        cin >> in[i];
    }
    ans = ini();
    cout << ans + in[n] << "\n";
    loop(i, 0, q)
    {
        int a, b;
        cin >> a >> b;
        ll av = in[a], bv = in[b];

        //  swap(in[a],in[b]);
        //  ll val = ini();
        //  dis(in);
        //  cout<<val+in[n]<<"\n";
        //  swap(in[a],in[b]);
        if (abs(a - b) > 1)
        {
            del(a);
            del(b);
            ins(a, bv);
            ins(b, av);
        }
        else if (abs(a - b) == 1)
        {

            if (in[b] > in[a])
            {
                //rem min
                if (!single(a))
                {
                    ans += in[a] - in[a - 1];
                }
                //rem max
                if (!single(b))
                {
                    ans += in[b] - in[b + 1];
                }
                ans += in[b] - in[a];
                swap(in[a], in[b]);
                //merge left
                if (in[a - 1] > in[a])
                {
                    ans -= in[a] - in[a - 1];
                }
                //merge right
                if (in[b + 1] < in[b])
                {
                    ans += in[b] - in[b - 1];
                }
            }
            else{
                ans -= in[b] - in[a];
                swap(in[a], in[b]);
            }

        }
        //    if(val != ans){
        //      dis(in);
        //    }
        cout << ans + in[n] << "\n";
    }
}
int main()
{

    io;

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int test;
    cin >> test;
    // test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}


// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
const int maxm = 100001;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
//NOT FOR NEGATIVE NUMBERS
// void sc(int &number)
// {
//     register long long c;
//     number = 0;
//     c = getchar();
//     for (; (c>47 && c<58); c=getchar())
//         number = number *10 + c - 48;

// }

void dis(vector<ll> &a)
{

    loop(i, 1, a.size() - 1)
    {
        cout << a[i] << " ";
    }
    cout << "\n";
}

vector<ll> in;
ll ans;
int n, q;
int maxv, minv;
ll ini()
{
    ll a = 0;
    loop1(i, 1, n)
    {
        a += in[i];
        int j = i + 1;
        for (; j <= n; j++)
        {
            if (in[j - 1] < in[j])
                break;
        }
        a -= in[j - 1];
        i = j - 1;
    }
    return a;
}

void del(int i)
{
    //elem only a single one
    ///do nothing

    if(!in[i+1]){
        //elem is min
        if (in[i - 1] > in[i])
        {
            ans += in[i] - in[i - 1];
        }
        in[i]=0;
        return;
    }
    if (!in[i - 1])
    {
        //elem is max
        if (in[i] > in[i + 1])
        {
            ans -= (in[i] - in[i + 1]);
        }
        in[i]=0;
        return;
    }

    //elem is min
    if (in[i - 1] > in[i] && in[i] < in[i + 1])
    {
        ans += in[i] - in[i - 1];
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i] > in[i + 1])
    {
        ans -= (in[i] - in[i + 1]);
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans -= in[i - 1];
        ans += in[i + 1];
    }
    in[i] = 0;
}
void ins(int i, ll val)
{
    in[i] = val;
    //elem is min

    if (!in[i + 1])
    {
        //elem is min
        if (in[i - 1] > in[i])
        {
            ans -= (in[i] - in[i - 1]);
        }
        return;
    }
    if (!in[i - 1])
    {
        //elem is max
        if (in[i] > in[i + 1])
        {
            ans += (in[i] - in[i + 1]);
        }
        return;
    }

    if (in[i - 1] > in[i] && in[i] < in[i + 1])
    {
        ans -= (in[i] - in[i - 1]);
    }
    //elem is max
    if (in[i - 1] < in[i] && in[i] > in[i + 1])
    {
        ans += in[i] - in[i + 1];
    }
    //elem is mid
    if (in[i - 1] > in[i] && in[i] > in[i + 1])
    {
        ans += in[i - 1];
        ans -= in[i + 1];
    }
}

int single(int i)
{
    return in[i - 1] < in[i] && in[i] < in[i + 1];
}
void solve()
{
    cin >> n >> q;
    in.assign(n + 2, 0);
    minv = 0;
    maxv = n + 1;
    ans = minv;
    in[n + 1] = maxv;
    loop1(i, 1, n)
    {
        cin >> in[i];
    }
    ans = ini();
    cout << ans + in[n] << "\n";
    loop(i, 0, q)
    {
        int a, b;
        cin >> a >> b;
        ll av = in[a], bv = in[b];

        //  swap(in[a],in[b]);
        //  ll val = ini();
        //  dis(in);
        //  cout<<val+in[n]<<"\n";
        //  swap(in[a],in[b]);

        if (b-a)
        {
            del(a);
            del(b);
            ins(a, bv);
            ins(b, av);
        }
        cout << ans + in[n] << "\n";
    }
}
int main()
{

    io;

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int test;
    cin >> test;
    // test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}
// 1
// 11 11
// 3 2 1 7 6 4 8 11 10 9 5
// 9 10
// 9 10
// 3 4
// 3 4
// 10 10
// 8 9
// 8 9
// 1 7
// 1 7
// 1 2
// 1 2

        // else if (abs(a - b) == 1)
        // {

        //     if (in[b] > in[a])
        //     {
        //         //rem min

        //         if (!single(a))
        //         {
        //             ans += in[a] - in[a - 1];
        //         }
        //         //rem max
        //         if (!single(b))
        //         {
        //             ans -= (in[b] - in[b + 1]);
        //         }
        //         ans += in[b] - in[a];
        //         swap(in[a], in[b]);
        //         //merge left
        //         if (in[a - 1] > in[a])
        //         {
        //             ans -= (in[a] - in[a - 1]);
        //         }
        //         //merge right
        //         if (in[b + 1] < in[b])
        //         {
        //             ans += in[b] - in[b + 1];
        //         }
        //     }
        //     else{
        //         //if left is max

        //         // erase max
        //         int lm = ;
        //         int rm =
        //         if(in[a-1]<in[a] && in[a]>in[a+1]){
        //           ans -= in[a];
        //         }else

        //         if (in[b - 1] > in[b] && in[b] < in[b + 1])
        //         {
        //             // erase min
        //           ans += in[b];
        //         }
        //         swap(in[a], in[b]);

        //         //merge left
        //         //elem is min
        //         if (in[a - 1] > in[a] && in[a] < in[a + 1]){
        //             ans -= (in[a] - in[a - 1]);
        //         }
        //         //merge right
        //         //elem is max
        //         if (in[b - 1] < in[b] && in[b] > in[b + 1])
        //         {
        //             ans += in[b] - in[b + 1];
        //         }
        //     }

        // }


// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
const int maxm = 100001;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
//NOT FOR NEGATIVE NUMBERS
// void sc(int &number)
// {
//     register long long c;
//     number = 0;
//     c = getchar();
//     for (; (c>47 && c<58); c=getchar())
//         number = number *10 + c - 48;

// }

// void dis(vi &a)
// {
//     cout << "\n....Display starts...\n";
//     loop(i, 0, a.size())
//     {
//         cout << a[i] << " ";
//     }
//     cout << "\n....Display ends...\n";
// }

// ll fe(ll base, ll exp) {
//     ll res=1;
//     while(exp>0) {
//       if(exp%2==1) res=(res*base)%MOD;
//       base=(base*base)%MOD;
//       exp/=2;
//     }
//     return res%MOD;
// }

void buildGraph(int n)
{
    adj = vector<vi>(n + 1);
    h = vector<int>(n + 1);
    sz = vector<int>(n + 1);
    loop(i, 0, n - 1)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
}
ll add(ll a, ll b)
{
    return ((a % m) + (b % m)) % m;
}
ll mul(ll a, ll b)
{
    return ((a % m) * (b % m)) % m;
}
ll sub(ll a, ll b)
{
    return ((a % m) - (b % m) + m) % m;
}
void solve()
{
    int n, q;
    cin >> n >> q;
    ll a[n];
    loop(i, 0, n)
    {
        cin >> a[i];
    }
    ll sum = 0;
    loop(i, 0, n)
    {
        //if last element then simply add
        if (i == n - 1)
        {
            sum += a[i];
            continue;
        }
        // not the last element
        //find smallest elemnent in seq
        int j = i + 1;
        for (; j < n; j++)
        {
            if (a[j - 1] > a[j])
                continue;
            else
                break;
        }

        if (j > i + 1)
        {
            sum += a[i];
            if (j != n)
            {
                sum -= a[j - 1];
            }
        }
        i = j - 1;
    }
    cout << sum << "\n";
}
int main()
{
    io;

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int test;
    cin >> test;
    // test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}