#include <bits/stdc++.h>
#define TASK "1343F"
#define pb push_back
#define ii pair<int,int>
#define ll long long
#define F first
#define S second
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define FOr(i, a, b) for (int i=a; i<b ; i++)
#define FOD(i, a, b) for (int i=a; i>=b; i--)
#define FOd(i, a, b) for (int i=a; i>b ; i--)
using namespace std;
const int N=209;
int a[N][N], b[N][N];
int ft[N], sz[N], ok[N], cnt[N];;
vector<int> ans;
vector<ii> can;
int t,n;
void n_3() {
    freopen(TASK".INP","r",stdin);
    freopen(TASK".OUT","w",stdout);
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            sz[i]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                if (l==2) can.pb({u,i});
                a[i][u]=1;
            }
        }
        FOR(i,0,n)
            FOR(j,0,n)
                b[i][j]=a[i][j];
        FOR(x,0,can.size()-1) {
            FOR(i, 1, n-1)
                ft[i]=0;
            int val=can[x].F;
            int fp=can[x].S;
            FOR(i,0,n)
            FOR(j,0,n)
                a[i][j]=b[i][j];
            ans.clear();
            ans.pb(val);
            vector<int> nxt;
            //ans.pb(val2);
            FOR(now, 2, n) {
                nxt.clear();
                FOR(i, 1, n-1)
                    if (a[i][val]) {
                        a[i][val]=0;
                        a[i][0]--;
                        if (a[i][0]==1) nxt.pb(i);
                        if (!ft[i]) ft[i]=now-1;
                    }
            if (nxt.size() == 0 || nxt.size() >1) break;
            fp=nxt[0];
            if (sz[fp] != now - ft[fp] + 1) break;
            FOR(j, 1, n)
                if (a[fp][j]==1) val=j;
            ans.pb(val);
            }
            if (ans.size()==n) break;
        }
        FOR(i, 0, n)
            FOR(j, 0, n)
                a[i][j]=0;
        FOR(i, 0, n-1)
            cout << ans[i] << " ";
        cout<<"\n";
    }
}
void n_2() {
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                ++cnt[u];
                a[i][u]=1;
            }
        }
        vector<int> try_;
        FOR(i,1,n)
            if (cnt[i]==1) try_.pb(i);
        for(int vlast:try_) {
            vector<int> ans(n);
            vector<int> used(n+1,0);
            int pnow;
            FOR(i, 1, n) cnt[i]=ft[i]=0;
            FOR(i,1,n)
                FOR(j,1,n)
                    if (a[i][j]) cnt[j]++;

            FOD(i, n-2, 1) {
                ans[i+1]=vlast;
                FOR(j, 1, n-1)
                    if (a[j][vlast]==1 && !ok[j])
                        pnow=j;
                ok[pnow]=1;
                used[pnow]=1;

                FOR(j, 1, n)
                    if (a[pnow][j]) {
                        cnt[j]--;
                        if (cnt[j]==1) vlast=j;
                        ft[j]=max(ft[j],i+1-a[pnow][0]+1);
                    }

            }
            FOR(i, 1, n-1)
                if (used[i] == 0 && a[i][0]!=2) continue;
            FOR(i, 1, n-1)
                ok[ans[i]]=2;
            vector<int> tp;
            FOR(i, 1, n)
                if (ok[i]!=2) tp.pb(i);
            ans[1]=tp[1];
            ans[0]=tp[0];
            if (ft[tp[0]] > 1) ans[1]=tp[0] ,ans[0]=tp[1];
            FOR(i, 0, n-1)
                cout<<ans[i]<<" ";
            cout<< "\n";
            FOR(i, 1, n)
                cnt[i]=ft[i]=ok[i]=0;
            FOR(i, 0, n)
                FOR(j, 1, n)
                    a[i][j]=0;
            break;
        }
    }
}
int main() {
    n_2();
}


#include <bits/stdc++.h>
#define TASK "1343F"
#define pb push_back
#define ii pair<int,int>
#define ll long long
#define F first
#define S second
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define FOr(i, a, b) for (int i=a; i<b ; i++)
#define FOD(i, a, b) for (int i=a; i>=b; i--)
#define FOd(i, a, b) for (int i=a; i>b ; i--)
using namespace std;
const int N=309;
int a[N][N], b[N][N];
int ft[N], sz[N];
vector<int> p[N],ans;
vector<ii> can;
int t,n;
int main()
{
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            sz[i]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                if (l==2) can.pb({u,i});
                a[i][u]=1;
                p[u].push_back(i);
            }
        }
        FOR(i,0,n)
            FOR(j,0,n)
                b[i][j]=a[i][j];
        FOR(x,0,can.size()-1) {
            FOR(i, 1, n-1)
                ft[i]=0;
            int val=can[x].F;
            int fp=can[x].S;
            FOR(i,0,n)
            FOR(j,0,n)
                a[i][j]=b[i][j];
            ans.clear();
            ans.pb(val);
            vector<int> nxt;
            //ans.pb(val2);
            FOR(now, 2, n) {
                nxt.clear();
                FOR(i, 1, n-1)
                    if (a[i][val]) {
                        a[i][val]=0;
                        a[i][0]--;
                        if (a[i][0]==1) nxt.pb(i);
                        if (!ft[i]) ft[i]=now-1;
                    }
            if (nxt.size() == 0 || nxt.size() >1) break;
            fp=nxt[0];
            if (sz[fp] != now - ft[fp] + 1) break;
            FOR(j, 1, n)
                if (a[fp][j]==1) val=j;
            ans.pb(val);
            }
            if (ans.size()==n) break;
        }
        FOR(i, 0, n)
            FOR(j, 0, n)
                a[i][j]=0;
        FOR(i, 0, n-1)
            cout << ans[i] << " ";
        cout<<"\n";
    }
}


#include <bits/stdc++.h>
#define TASK "1343F"
#define pb push_back
#define ii pair<int,int>
#define ll long long
#define F first
#define S second
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define FOr(i, a, b) for (int i=a; i<b ; i++)
#define FOD(i, a, b) for (int i=a; i>=b; i--)
#define FOd(i, a, b) for (int i=a; i>b ; i--)
using namespace std;
const int N=209;
int a[N][N], b[N][N];
int ft[N], sz[N];
vector<int> p[N],ans;
vector<ii> can;
int t,n;
int main()
{
    //freopen(TASK".INP","r",stdin);
    //freopen(TASK".OUT","w",stdout);
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            sz[i]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                if (l==2) can.pb({u,i});
                a[i][u]=1;
                p[u].push_back(i);
            }
        }
        FOR(i,0,n)
            FOR(j,0,n)
                b[i][j]=a[i][j];
        FOR(x,1,can.size()-1) {
            FOR(i, 1, n-1)
                ft[i]=0;
            int val=can[x].F;
            //int val2;
            //if (x%2) val2=can[x-1].F;
                //else val2=can[x+1].F;
            int fp=can[x].S;
            FOR(i,0,n)
            FOR(j,0,n)
                a[i][j]=b[i][j];
            ans.clear();
            ans.pb(val);
            vector<int> nxt;
            //ans.pb(val2);
            FOR(now, 2, n) {
                nxt.clear();
                FOR(i, 1, n-1)
                    if (a[i][val]) {
                        a[i][val]=0;
                        a[i][0]--;
                        if (a[i][0]==1) nxt.pb(i);
                        if (!ft[i]) ft[i]=now-1;
                    }
            if (nxt.size() == 0 || nxt.size() >1) break;
            fp=nxt[0];
            if (sz[fp] != now - ft[fp] + 1) break;
            FOR(j, 1, n)
                if (a[fp][j]==1) val=j;
            ans.pb(val);

            }
            if (ans.size()==n) break;
        }
        FOR(i, 0, n)
            FOR(j, 0, n)
                a[i][j]=0;
        FOR(i, 0, n-1)
            cout << ans[i] << " ";
        cout<<"\n";
    }
}


#include <bits/stdc++.h>
#define TASK "1343F"
#define pb push_back
#define ii pair<int,int>
#define ll long long
#define F first
#define S second
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define FOr(i, a, b) for (int i=a; i<b ; i++)
#define FOD(i, a, b) for (int i=a; i>=b; i--)
#define FOd(i, a, b) for (int i=a; i>b ; i--)
using namespace std;
const int N=209;
int a[N][N],b[N][N];
vector<int> p[N],ans;
vector<ii> can;
int t,n;
int main()
{
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                if (l==2) can.pb({u,i});
                a[i][u]=1;
                p[u].push_back(i);
            }
        }
        FOR(i,0,n)
            FOR(j,0,n)
                b[i][j]=a[i][j];
        FOR(x,1,can.size()-1) {
            int val=can[x].F;
            //int val2;
            //if (x%2) val2=can[x-1].F;
                //else val2=can[x+1].F;
            int fp=can[x].S;
            FOR(i,0,n)
            FOR(j,0,n)
                a[i][j]=b[i][j];
            ans.clear();
            ans.pb(val);
            vector<int> nxt;
            //ans.pb(val2);
            FOR(now, 2, n) {
                nxt.clear();
                FOR(i, 1, n-1)
                    if (a[i][val]) {
                        a[i][val]=0;
                        a[i][0]--;
                        if (a[i][0]==1) nxt.pb(i);
                    }
            if (nxt.size() == 0 || nxt.size() >1) break;
            fp=nxt[0];
            FOR(j, 1, n)
                if (a[fp][j]==1) val=j;
            ans.pb(val);

            }
            if (ans.size()==n) break;
        }
        FOR(i, 0, n)
            FOR(j, 0, n)
                a[i][j]=0;
        FOR(i, 0, n-1)
            cout << ans[i] << " ";
        cout<<"\n";
    }
}


#include <bits/stdc++.h>
#define TASK "1343F"
#define pb push_back
#define ii pair<int,int>
#define ll long long
#define F first
#define S second
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define FOr(i, a, b) for (int i=a; i<b ; i++)
#define FOD(i, a, b) for (int i=a; i>=b; i--)
#define FOd(i, a, b) for (int i=a; i>b ; i--)
using namespace std;
const int N=209;
int a[N][N], b[N][N];
int ft[N], sz[N];
vector<int> p[N],ans;
vector<ii> can;
int t,n;
int main()
{
    //freopen(TASK".INP","r",stdin);
    //freopen(TASK".OUT","w",stdout);
    cin>>t;
    while(t--) {
        cin>>n;
        FOR(i,1,n-1) {
            int l;
            cin>>l;
            a[i][0]=l;
            sz[i]=l;
            FOR(j, 1, l) {
                int u;
                cin>>u;
                if (l==2) can.pb({u,i});
                a[i][u]=1;
                p[u].push_back(i);
            }
        }
        FOR(i,0,n)
            FOR(j,0,n)
                b[i][j]=a[i][j];
        FOR(x,0,can.size()-1) {
            FOR(i, 1, n-1)
                ft[i]=0;
            int val=can[x].F;
            //int val2;
            //if (x%2) val2=can[x-1].F;
                //else val2=can[x+1].F;
            int fp=can[x].S;
            FOR(i,0,n)
            FOR(j,0,n)
                a[i][j]=b[i][j];
            ans.clear();
            ans.pb(val);
            vector<int> nxt;
            //ans.pb(val2);
            FOR(now, 2, n) {
                nxt.clear();
                FOR(i, 1, n-1)
                    if (a[i][val]) {
                        a[i][val]=0;
                        a[i][0]--;
                        if (a[i][0]==1) nxt.pb(i);
                        if (!ft[i]) ft[i]=now-1;
                    }
            if (nxt.size() == 0 || nxt.size() >1) break;
            fp=nxt[0];
            if (sz[fp] != now - ft[fp] + 1) break;
            FOR(j, 1, n)
                if (a[fp][j]==1) val=j;
            ans.pb(val);

            }
            if (ans.size()==n) break;
        }
        FOR(i, 0, n)
            FOR(j, 0, n)
                a[i][j]=0;
        FOR(i, 0, n-1)
            cout << ans[i] << " ";
        cout<<"\n";
    }
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>

void main()
{
	int a,b,c,d[15],e=0,f;
	scanf("%d",&a);

	for(b=0;b<a/2+1;b++)
	{
		for(c=0;c<a/2-b;c++)
			printf("*");
		for(c=0;c<1+b*2;c++)
			printf("D");
		for(c=0;c<a/2-b;c++)
			printf("*");
		printf("\n");
	}
	for(b=0;b<a/2;b++)
	{
		for(c=0;c<b+1;c++)
			printf("*");
		for(c=0;c<a-2*b-2;c++)
			printf("D");
		for(c=0;c<b+1;c++)
			printf("*");
		printf("\n");

	}
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int main()
{
//	freopen("Crystal Mine.in","r",stdin);
//	freopen(".out","w",stdout);
	int n;
	cin>>n;
	int t=(n-1)/2,p=(n+1)/2;
	For(i,n)
	{
		For(j,n)
			if (abs(p-i)+abs(p-j)<=t) printf("D");else printf("*");

		cout<<endl;
	}

	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=1;i<n/2+1;++i)
		{
			for(int j=0;j<n/2+1-i;++j)
				cout<<"*";
			for(int j=0;j<2*i-1;++j)
				cout<<"D";
			for(int j=0;j<n/2+1-i;++j)
				cout<<"*";
			cout<<endl;
		}
		for(int i=0;i<n;++i)
			cout<<"D";
		cout<<endl;
		for(int i=n/2;i>0;--i)
		{
			for(int j=0;j<n/2+1-i;++j)
				cout<<"*";
			for(int j=0;j<2*i-1;++j)
				cout<<"D";
			for(int j=0;j<n/2+1-i;++j)
				cout<<"*";
			cout<<endl;
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int n;
int main(){
    cin >> n;
    int t=0;
    for (int i=1;i<=n/2;++i) {
        int t=n/2-i+1;
        for (int j=1;j<=t;++j) cout <<"*";
        for (int j=1;j<=n-t*2;++j) cout <<"D";
        for (int j=1;j<=t;++j) cout <<"*";
        cout << endl;
    }
    for (int i=1;i<=n;++i) cout <<"D";
    cout <<endl;
    for (int i=1;i<=n/2;++i) {
        for (int j=1;j<=i;++j) cout <<"*";
        for (int j=1;j<=n-i*2;++j) cout <<"D";
        for (int j=1;j<=i;++j) cout <<"*";
        cout << endl;
    }
    return 0;
    }


# include <iostream>
using namespace std;

int main()
{

	int n ,  d =1  , s  ;
	cin >> n ;
	s = n / 2 ;

	for ( int i = 1 ; i <= n ; ++ i )
	{
		int s2 = s ;
		while ( s2 )
		{
			cout <<'*' ;
			-- s2 ;
		}
		int d2 = d;

		while ( d2 )
		{
			cout << 'D' ;
			-- d2 ;
		}

		s2 = s ;
		while ( s2 )
		{
			cout <<'*' ;
			-- s2 ;
		}
		cout << endl ;

		if ( i < n / 2 )
		{
			-- s ;
			d += 2 ;
		}

		else if ( i > n/2 )
			{
				++ s ;
				d -= 2 ;
			}
			else if  ( i == n / 2 )
				{
					s = 0 ;
					d = n ;
				}

	}

	//system ("pause>null" ) ;

	return false;
}


# include <iostream>
using namespace std;

int main()
{

	int n ,  d =1  , s  ;
	cin >> n ;
	s = n / 2 ;

	for ( int i = 0 ; i < n ; ++ i )
	{
		int s2 = s ;
		while ( s2 )
		{
			cout <<'*' ;
			-- s2 ;
		}
		int d2 = d;

		while ( d2 )
		{
			cout << 'D' ;
			-- d2 ;
		}

		s2 = s ;
		while ( s2 )
		{
			cout <<'*' ;
			-- s2 ;
		}
		cout << endl ;

		if ( i < n / 2 )
		{
			-- s ;
			d += 2 ;
		}

		else if ( i > n/2 )
			{
				++ s ;
				d -= 2 ;
			}
			else if  ( i == n / 2 )
				{
					s = 0 ;
					d = n ;
				}

	}

	//system ("pause>null" ) ;

	return false;
}


#include <iostream>
using namespace std;
int main()
{
	int a,b,c,i;
	cin>>a;
	b=2*((a+1)/2)-1;
	c=b;
	b=b/2;
	for(i=1;i<(a+1)/2;i++)
	{
		c=2*i-1;
		for(int j=1;j<=b;j++)
			cout<<"*";
		for(int k=1;k<=c;k++)
			cout<<"D";
		for(int z=b;z>0;z--)
			cout<<"*";
		cout<<endl;
		b--;
	}
	for(int k=0;k<a;k++)
		cout<<"D";
	cout<<endl;
	b++;
	for(i-=1;i>0;i--)
	{
		c=2*i-1;
		for(int z=b;z>0;z--)
			cout<<"*";
		for(int k=1;k<=c;k++)
			cout<<"D";
		for(int j=1;j<=b;j++)
			cout<<"*";
		cout<<endl;
		b++;
	}
	return 0;
}


n=int(input())
k=[]
s=''
a=n-1
a=a//2
b=1
p=(n//2)+1
for i in range(p):
    for j in range(a):
        s+='*'
    for j in range(b):
        s+='D'
    for j in range(a):
        s+='*'
    k.append(s)
    s=''
    b=b+2
    a=a-1
s=''
a=1
b=n-2
for i in range(p-1):
    for j in range(a):
        s+='*'
    for j in range(b):
        s+='D'
    for j in range(a):
        s+='*'
    k.append(s)
    s=''
    b=b-2
    a=a+1
for i in k:
    print(i)


n=int(input())
for i in range(n):
    a=abs(n//2-i)
    c='*'*a
    b='D'*(n-2*a)
    print(c+b+c)


n=int(input())
s=(n-1)//2
c=1
for i in range(n//2+1):
    for j in range(s):
        print('*',end='')
    for j in range(c):
        print('D',end='')
    for j in range(s):
        print('*',end='')
    s-=1
    c+=2
    print()
s+=2
c-=4
for i in range(n//2):
    for j in range(s):
        print('*',end='')
    for j in range(c):
        print('D',end='')
    for j in range(s):
        print('*',end='')
    s+=1
    c-=2
    print()


#!/bin/python3
n = int(input())
for row in range(n):
	sym = 0
	if row < n - 1 - row:
		sym = row
	else:
		sym = (n - 1 - row)
	for k in range(int((n - 1) / 2 - sym)):
		print("*", end="")
	for k in range(2 * sym + 1):
		print("D", end="")
	for k in range(int((n - 1) / 2 - sym)):
		print("*", end="")
	print()


n = int(input())
for i in range(n):
    for ii in range(n):
        if min(ii, n-ii-1) >= (abs(n//2-i)):
            print("D", end="")
        else:
            print("*", end="")
    print()


n = int(input())

space = int((n-1) / 2)
diamond = 1

for j in range(1, n+1):

    print("*"*space, end="")

    print("D"*diamond, end="")

    print("*"*space, end="")

    print()

    if j <= (n-1) / 2:
        space -= 1
        diamond += 2
    else:
        space += 1
        diamond -= 2


n=int(input())
for i in range(n):
    if(i>n//2):
        i=n-1-i
    if(i==n//2):
        for j in range(n):
            print("D",end="")
        print()
    else:
        for j in range(n):
            if(j>=n//2-i and j<=n//2+i):
                print("D",end="")
            else:
                print("*",end="")
        print()


n = int(input())
for i in range(n):
    m = n // 2
    if i > m:
        t = i - m
    else:
        t = m - i
    print(t * '*' + (n - t - t) * 'D' + t * '*')


n = int(input())
for i in range(n):
	m = n / 2
	if i > m:
		t = i - m
	else:
		t = m - i
	print(['*'] * t)
	print(['*'] * (n - t - t))
	print(['*'] * t)


#!/usr/bin/env python3
import re
n = int(input())

stars = n // 2
line = n * 'D'
mines = []
for i in range(1, len(line) // 2 + 1):
	mines.insert(0, ((i * '*') + line[i:len(line) - i] + (i * '*')))

for i in range(len(mines)):
	print(mines[i])

print(line)

for i in range(len(mines) - 1, -1, -1):
	print(mines[i])


#!/usr/bin/env python3
n = int(input())

stars = n // 2
line = n * 'D'
mines = []
for i in range(1, len(line) // 2 + 1):
	mines.insert(0, ((i * '*') + line[i:len(line) - i] + (i * '*')))

for i in range(len(mines)):
	print(mines[i])

print(line)

for i in range(len(mines) - 1, -1, -1):
	print(mines[i])


#!/usr/bin/env python3
import re
n = int(input())

stars = n // 2
line = 7 * 'D'
mines = []
for i in range(1, len(line) // 2 + 1):
	mines.insert(0, ((i * '*') + line[i:len(line) - i] + (i * '*')))

for i in range(len(mines)):
	print(mines[i])

print(line)

for i in range(len(mines) - 1, -1, -1):
	print(mines[i])


n = int(input())

for i in range(n):
    for j in range(n):
        if min(i, n - 1 - i) + min(j, n - 1 - j) < n // 2:
            print('*', end = '')
        else:
            print('D', end = '')
    print()


n=int(input())
for i in range(1,((n+1)//2)+1):
    for j in range(1,n+1):
        c=(n+1)//2
        if  j>=c-(i-1) and j<=c+(i-1):
            print('D',end='')
        else:print('*',end='')
    print()
for i in range(((n+1)//2)+1,n+1):
    for j in range(1,n+1):
        c=(n+1)//2
        if j>=c-(n-i) and j<=c+(n-i):print('D',end='')
        else:print('*',end='')
    print()


els = []
i = 0
n = int(input())
for _ in range(n//2+1):
    a = (n - (2*i) - 1)//2
    el = '*'*a + 'D'*(2*i+1) + '*'*a
    i += 1
    els.append(el)
    print(el)
for i in range(len(els) - 2, -1, -1):
    print(els[i])


# Little Pony and Crystal Mine
# http://codeforces.com/problemset/problem/454/A

n = int(input())

string = "D"

x=1
while x < n:
    print(string.center(n,"*"))
    string = string + "DD"
    x = x + 2
# print(string + "DD")
while x > 1:
    string = string[:-2]
    print(string.center(n,"*"))
    x = x - 2


# Little Pony and Crystal Mine
# http://codeforces.com/problemset/problem/454/A

n = int(input())

string = "D"

x=1
while x < n:
    print(string.center(n,"*"))
    string = string + "DD"
    x = x + 2
print(string)
while x > 1:
    string = string[:-2]
    print(string.center(n,"*"))
    x = x - 2


def solve():
    n = int(input())
    score = n // 2
    lst = []
    for i in range(score, -1, -1):
        string = "*" * i + "D" * (n - 2*i) + "*" * i
        lst.append(string)

    for j in lst:
        print(j)
    for index, k in enumerate(lst[::-1]):
        if index == 0:
            continue
        print(k)

solve()


def solve():
    n = int(input())
    score = n // 2
    lst = []
    for i in range(score, -1, -1):
        string = "*" * i + "D" * (n - 2*i) + "*" * i
        lst.append(string)

    for j in lst:
        print(j)
    for k in lst[1::-1]:
        print(k)

solve()


n = int(input())
lst = ['*']*n
lst_copy = lst.copy()
mid = n//2
lstn = []
for i in range(mid+1):
    lst[mid-i:mid+i+1] = ['D']*(2*i+1)
    lstn.append(''.join(lst))
    lst = lst_copy.copy()
for j in lstn:
    print(j)
for i in range(2,mid+2):
    print(lstn[-i])


# https://codeforces.com/contest/454/problem/A

n = int(input())

j = 1
i = n // 2
k = 0
while k < n:
    if k < n //2:
        print("*"*i+"D"*j+"*"*i)
        j += 2
        i -= 1
        if j > n:
            j -= 2
            i += 1
    else:
        print("*" * i + "D" * j + "*" * i)
        j -= 2
        i += 1
    k += 1


# import sys
# sys.stdin=open("input.in",'r')
# sys.stdout=open("outp.out",'w')
n=int(input())
x=int(n/2)
for i in range(1,n+1):
	if x==0:
		x=1
	elif x<0:
		x=0
	print("*"*x+'D'*(n-(2*x))+'*'*x)
	if i <=int(n/2) :
		x-=i
	else :
		x+=i-int(n/2)-1
	# print(x,i)


# import sys
# sys.stdin=open("input.in",'r')
# sys.stdout=open("outp.out",'w')
n=int(input())
x=int(n/2)
for i in range(1,n+1):
	print("*"*x+'D'*(n-(2*x))+'*'*x)
	if i <=int(n/2) :
		x-=1
	else :
		x+=1
	# print(x,i)


n = int(input())
numD = 1
for i in range(n):
	print((n-numD)//2*'*'+numD*'D'+(n-numD)//2*'*')
	if i<n//2:
		numD+=2
	else:
		numD-=2


n = int(input())
for i in range(n):
    if i < n //2:
        w = ((i+1)*2) -1
    else:
        w = ((n- i)*2) -1
    print('{:*^{width}}'.format('D'*w, width=n))


n = int(input())
for i in range(n):
    if i < n //2:
        w = ((i+1)*2) -1
    else:
        w = ((n- i)*2) -1
    print('{:*^{width}}'.format('C'*w, width=n))


amount = int(input())

for i in range(amount//2):
    print("*"* (amount//2-i), end="")
    print("D"*(i*2+1), end="")
    print("*" * (amount // 2-i))

print("D"*amount)

for i in range(amount//2):
    print("*" * (i+1), end="")
    print("D" * (amount - 2 - (i*2)), end="")
    print("*" * (i+1))


n = int(input())

line = ['*' for x in range(n)]
for x in range(n):
    print(''.join(line))


n = int(input())

counter = 3

line = ['*' for x in range(n)]
h = int(n/2)
line[h] = 'D'
first_line = ''.join(line)

print(first_line)

lim = False

for x in range(n-2):
    line[h] = 'D'
    c = 1
    for y in range(h):
        y += 1
        if counter > c:
            line[h-y] = line[h+y] = 'D'
            c += 2
        else:
            line[h-y] = line[h+y] = '*'
    if lim:
        counter -= 2
    else:
        counter += 2
    if counter == n:
        lim = True

    print(''.join(line))

print(first_line)


n = int(input())

line = ['*' for x in range(n)]
h = int(n/2)
line[h] = 'D'
for x in range(n):
    print(''.join(line))


n = int(input())
for i in range(0, n):
    a = abs(n//2 - i)
    print("*"*a + "D"*(n-2*a) + "*"*a)


n = int(input())
k=0
p=1
t=0
for i in range(n):
    for j in range(n//2-k):
        print("*",end='')
    for j in range(p):
        print("D", end='')
    for j in range(n//2-k):
        print("*",end='')
    print(end='\n')
    if p!=n and t==0:
        k+=1
        p+=2
    else:
        t=1
        k-=1
        p-=2


n = int(input())
k=0
p=1
t=0
for i in range(n):
    for j in range(n//2-k):
        print("*",end='')
    for j in range(p):
        print("D", end='')
    for j in range(n//2-k):
        print("*", end='')
    print("\n")
    if p!=n and t==0:
        k+=1
        p+=2
    else:
        t=1
        k-=1
        p-=2


n = int(input())
for i in range(n):
	temp = min(i, n - i - 1)
	a = n // 2 - temp
	for j in range(a):
		print('*', end = '')
	for j in range(temp * 2 + 1):
		print('D', end = '')
	for j in range(a):
		print('*', end = '')
	print()


n = int(input())

for i in range(n):
	temp = min(i, n - i - 1)
	for j in range(n // 2 - temp):
		print('*', end = '')
	for j in range(temp * 2 + 1):
		print('D', end = '')
	for j in range(n // 2 - temp):
		print('*', end = '')
	print()


n,m=map(int,input().split())
S = set()
for i in range(n):
    l = input().split()
    for x in l[1:]:
        S.add(int(x))

if len(S) == m:
    print("YES")
else:
    print("NO")


a=int(input())
l=[]
for x in range(1,a+1,2):
    s="D"*x
    t=((a-x)/2)
    z="*"*int(t)
    s=z+s+z
    l.append(str(s))
    pass

z=l[0:len(z)-1]
for x in z:
    print (x)
    pass

print ("D"*a)

# if a&1 == 1:
#     print ("D"*a)
# else:
#     print ("D"*(a-1))
#     pass

for x in reversed(z):
    print (x)
    pass


a=int(input())
l=[]
for x in range(1,a+1,2):
    s="D"*x
    t=((a-x)/2)
    z="*"*int(t)
    s=z+s+z
    l.append(str(s))
    pass

z=l[0:len(z)-1]
for x in z:
    print (x)
    pass

# if a&1 == 1:
#     print ("D"*a)
# else:
#     print ("D"*(a-1))
#     pass

for x in reversed(z):
    print (x)
    pass


#!/bin/python3
n = int(input())
for row in range(n):
	sym = 0
	if row < n - 1 - row:
		sym = row
	else:
		sym = (n - 1 - row)
	for k in range(int((n - 1) / 2 - sym)):
		print("*", end="")
	for k in range(2 * sym + 1):
		print("D", end="")
	for k in range(int((n - 1) / 2 - sym)):
		print("*", end="")
	print()


#### it's just a o(n)

n = int(input())
for i in range(1 , n+1 , 2):
    j = (n-i)//2
    print('*' *j , end='')
    print('D'*i , end='')
    print('*' *j)
for i in range(n-2 , 0 , -2):
    j = (n-i)//2
    print('*'*j,end='')
    print('D'*i,end='')
    print('*'*j)


n = int(input())
j = 1
mid = False
for i in range(n):
    if i == (n-1)//2 :
        mid = True
    a = '*' * ((n-j)//2)
    d = 'D' * j
    print(a,d,a,sep='')
    if mid:
        j -= 2
    else :
        j += 2


n = int(input())
x = 1
for i in range(1, n+1):
    print(("D"*x).center(n, "*"))
    if i >= n // 2 + 1:
        x -= 2
    else:
        x += 2


import sys
input = sys.stdin.readline
n= int(input())
mid = int((n-1)/2)
for i in range(mid):
    print('%s%s%s' %('*'*(mid-i),'D'*(n-(mid-i)*2),'*'*(mid-i)))
print('D'*n)
for i in range(mid,n-1):
    print('%s%s%s' %('*'*(i-mid+1),'D'*(n-(i-mid+1)*2),'*'*(i-mid+1)))


n = int(input())
a = []
mid = n//2
for i in range(n):
  a.append([])
  x = abs(mid - i)
  for j in range(n):
    if j >= x and j <= n-x-1:
      a[i].append('D')
    else:
      a[i].append('*')
for i in range(n):
  print(*a[i])


n = int(input())
a = []
mid = n//2
for i in range(n):
    a.append([])
    x = abs(mid - i)
    for j in range(n):
        if j >= x and j <= n-x-1:
            a[i].append('D')
        else:
            a[i].append('*')
for i in range(n):
    for j in range(n):
        print(a[i][j], end='')
    print(end='\n')


n=int(input())
i=1
k=0
t=(n+1)/2
while i<=n:
    if i<=t:
        k=k+1
    else:
        k=k-1
    j=1
    while j<=n:
        if j>=t+1-k and j<=t-1+k:
            print("D",end="")
        else:
            print("*",end="")
        j=j+1

    print()
    i=i+1


import time
a=int(input())
b=a
h=""
while a>0:
	h=""
	c=a//2
	d=c
	o=b-(c*2)
	while c!=0:
		c=c-1
		h=h+"*"
	while o!=0:
		o=o-1
		h=h+"D"
	while d!=0:
		d=d-1
		h=h+"*"
	print(h)
	a=a-2
a=1
while a<b:
	a=a+2
	h=""
	c=a//2
	d=c
	o=b-(c*2)
	while c!=0:
		c=c-1
		h=h+"*"
	while o!=0:
		o=o-1
		h=h+"D"
	while d!=0:
		d=d-1
		h=h+"*"
	print(h)

time.sleep(100)


n = int(input())
k = n // 2
ans = [['*']*n for i in range(n)]
i = 0
while k >= 0:
    for j in range(k,n-k):
        ans[i][j] = 'D'
    k -= 1
    i += 1
k = 1
while k <=n // 2:
    for j in range(k,n-k):
        ans[i][j] = 'D'
    k += 1
    i += 1
for o in range(n):
    print(*ans[o])


n = int(input())
k = n // 2
ans = [['*']*n for i in range(n)]
i = 0
while k >= 0:
    for j in range(k,n-k):
        ans[i][j] = 'D'
    k -= 1
    i += 1
k = 1
while k <=n // 2:
    for j in range(k,n-k):
        ans[i][j] = 'D'
    k += 1
    i += 1
for i in range(n):
    for j in range(n):
        print(ans[i][j], end = '')
    print('')


#####--------------Template Begin-------------------####
import math
import sys
import string
#input = sys.stdin.readline
def ri(): #Regular input
	return input()
def ii(): #integer input
	return int(input())
def li(): #list input
	return input().split()
def mi(): #map input
	return list(map(int, input().split()))
#####---------------Template Ends-------------------######
n=ii()
for i in range(1, n-1, 2):
	print(("D"*i).center(n, "*"))

print("D"*n)

for i in range(n-2,0,-2):
	print(("D"*i).center(n, "*"))


#####--------------Template Begin-------------------####
import math
import sys
import string
#input = sys.stdin.readline
def ri(): #Regular input
	return input()
def ii(): #integer input
	return int(input())
def li(): #list input
	return input().split()
def mi(): #map input
	return list(map(int, input().split()))
#####---------------Template Ends-------------------######
n=ii()
for i in range(1, n, 2):
	print(("D"*i).center(n, "*"))
for i in range(n, 0, -2):
	print(("D"*i).center(n, "*"))


n=int(input())
for i in range(n//2):
	for j in range(n):
		if j<n//2-i or j>n//2+i:
			print('*',end='')
		else:
			print('D',end='')
	print()
for i in range(n//2,n):
	for j in range(n):
		if j<n//2 -n+1+i or j>n//2 +n-1-i:
			print('*',end='')
		else:
			print('D',end='')
	print()


n=int(input())
for i in range(n//2):
	for j in range(n):
		if j<n//2-i or j>n//2+i:
			print('*',end=' ')
		else:
			print('D',end=' ')
	print()
for i in range(n//2,n):
	for j in range(n):
		if j<n//2 -n+1+i or j>n//2 +n-1-i:
			print('*',end=' ')
		else:
			print('D',end=' ')
	print()


n = int(input())
a = ['{0}{1}{0}'.format('*' * ((n - i) // 2), 'D' * i) for i in range(1, n, 2)]
[print(i) for i in a]
print('D' * len(a[0]))
[print(i) for i in reversed(a)]


"""
instagram : essipoortahmasb2018
telegram channel : essi_python

"""
n=int(input())
a=[['*']*n for i in range(n)]
ind1=n//2
ind2=n//2+1
index=0
index2=n-1
for i in range(1,n+1,2):
    a[index][ind1:ind2]=i*'D'
    a[index2][ind1:ind2]=i*'D'
    ind1-=1
    ind2+=1
    index+=1
    index2-=1

for i in range(n):
    print(*a[i],sep='')


"""
instagram : essipoortahmasb2018
telegram channel : essi_python

"""
n=int(input())
a=[['*']*n for i in range(n)]
ind1=n//2
ind2=n//2+1
index=0
index2=n-1
for i in range(1,n+1,2):
    a[index][ind1:ind2]=i*'D'
    a[index2][ind1:ind2]=i*'D'
    ind1-=1
    ind2+=1
    index+=1
    index2-=1

for i in range(n):
    print(*a[i])


# import sys
# sys.stdin=open('input.in','r')
# sys.stdout=open('output.out','w')
n=int(input())
k=(n//2)+1
for x in range(1,(n//2)+1):
	print('*'*(k-x),end='')
	print('D'*(2*x-1),end='')
	print('*'*(k-x))
print('D'*n)
for x in range((n//2),0,-1):
	print('*'*(k-x),end='')
	print('D'*(2*x-1),end='')
	print('*'*(k-x))


n = int(input())
cristal = []
for i in range(n // 2):
	cristal.append(['*'] * n)
	for j in range((n // 2) - i, n - ((n // 2) - i)):
		cristal[i][j] = 'D'
cristal.append(['D'] * n)
for i in range((n // 2) - 1, -1, -1):
	cristal.append(cristal[i])
for i in range(n):
	print(*cristal[i], sep = '')


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

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


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

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

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


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

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


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

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

}


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

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

}


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

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

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

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


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

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

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


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

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


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

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

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


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

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


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

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


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

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


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

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


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

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


#include<iostream>

using namespace std;

#define int long long

int a[1000000+5];

int n;

int ans=0;

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

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

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


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

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


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

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