//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<pll> seg1,seg2,segs;
    for(ll i=0;i<n;i++){
        segs.pb({v[i],0});
    	while(i+1<n && v[i+1]>v[i])i++;
        segs.back().B=v[i];
    }
    x=segs.size();mx=v[n-1],mn=v[0];
    //ll chk1=x-1;ll chk2=0;
    /*repd(i,x-2,0){
    	if(segs[i].A>=mx){
    		break;
    	}
        chk1=i;
    }*/
    rep(i,1,x){
    	if(segs[i].B<=mn){
    		flg=0;
    	}
        else flg=1;
    }
    //if(mx>mn && chk1<=chk2)flg=1;
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<ll> seg1,seg2;
    for(ll i=0;i<n;i++){
        if(i!=n-1)seg1.pb(v[i]);
    	while(i+1<n && v[i+1]>v[i])i++;
        if(i!=0)seg2.pb(v[i]);
    }
    x=seg1.size();mx=v[n-1],mn=v[0];
    rep(i,0,x){
    	if(seg1[i]>=mx){
    		flg=0;
    		break;
    	}
    }
    rep(i,0,seg2.size()){
    	if(seg2[i]<=mn){
    		flg=0;
    		break;
    	}
    }
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=0;
    rep(i,0,n)cin>>v[i];
    vector<pll> seg1,seg2,segs;
    for(ll i=0;i<n;i++){
        segs.pb({v[i],0});
    	while(i+1<n && v[i+1]>v[i])i++;
        segs.back().B=v[i];
    }
    x=segs.size();mx=v[n-1],mn=v[0];
    ll chk1=x-1;ll chk2=0;
    repd(i,x-2,0){
    	if(segs[i].A>=mx){
    		break;
    	}
        chk1=i;
    }
    rep(i,1,x){
    	if(segs[i].B<=mn){
    		break;
    	}
        chk2=i;
    }
    if(mx>mn && chk1<=chk2)flg=1;
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<ll> seg1;
    for(ll i=0;i<n;i++){
    	if(i==n-1)break;
        seg1.pb(v[i]);
    	while(i+1<n && v[i+1]>v[i])i++;
    }
    x=seg1.size();mx=v[n-1];
    rep(i,0,x){
    	if(seg1[i]>=mx){
    		flg=0;
    		break;
    	}
    }
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<pll> seg1,seg2,segs;
    for(ll i=0;i<n;i++){
        segs.pb({v[i],0});
    	while(i+1<n && v[i+1]>v[i])i++;
        segs.back().B=v[i];
    }
    x=segs.size();mx=v[n-1],mn=v[0];
    rep(i,0,x-1){
    	if(segs[i].A>=mx){
    		flg=0;
    		break;
    	}
    }
    rep(i,1,x){
    	if(segs[i].B<=mn){
    		flg=0;
    		break;
    	}
    }
    if(mx<=mn)flg=0;
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<ll> seg1,seg2;
    for(ll i=0;i<n;i++){
    	seg1.pb(i);
    	while(i+1<n && v[i+1]>v[i])i++;
    	seg2.pb(i);
    }
    reverse(all(seg1));
    reverse(all(seg2));
    x=seg1.size();mx=v[n-1];
    rep(i,1,x){
    	if(v[seg1[i]]>=mx){
    		flg=0;
    		break;
    	}
    }
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


//Author: Naman Vidyabhanu

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

/*
   MY
   TEMPLATE
   BEGINS
   HERE
*/
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define A first
#define B second
#define all(v) v.begin(),v.end()
#define srt(v) sort(all(v))
#define gll greater<ll>()
#define rsrt(v) sort(all(v),gll)
#define GIVE_ME_SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);
#define stp(x) cout<<fixed<<setprecision(x)
#define umap unordered_map
#define br cout<<"\n"
#define mod (ll)(1e9+7)
#define rep(i,y,x) for(ll i=y;i<x;i++)
#define repd(i,y,x) for(ll i=y;i>=x;i--)
#define vll vector<ll>
#define vld vector<ld>
#define vs vector<string>
#define vc vector<char>
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define mll map<ll,ll>
#define mls map<ll,string>
#define msl map<string,ll>
#define mcl map<char,ll>
#define mlc map<ll,char>

ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

ll mypow(ll x,ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1)
            res = res*x;
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}

ll mylog(ll n,ll b){
    ll cnt=0;
    while(n>1)n/=b,cnt++;
    return cnt;
}

bool cmp(pair<ll,ll> p1,pair<ll,ll> p2){
    return true;
}

struct hash_pair{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
/*
	diagonal traversal of matrix */
//pair , size , diagonl, anti diagoal traversal, sieve, isprime, modex, bfs , dfs, isp2, siz, ginp;
//ll a,b,c,d,x,y,z,p,q,l,r,k,t=1,n,m,cnt,curr,prev,ndx,i,j,rem,cnt1,cnt2,tmp,nxt,sum,ans,mx,mn;

string s="",s1="",s2="";
/*
    MY
    TEMPLATE
    ENDS
    HERE
*/

// SOLUTION IN THE FOLLOWING FUNCTION
void solve(){
    cin>>n;
    vll v(n); ll flg=1;
    rep(i,0,n)cin>>v[i];
    vector<ll> seg1;
    for(ll i=0;i<n;i++){
    	seg1.pb(v[i]);
    	while(i+1<n && v[i+1]>v[i])i++;
    }
    x=seg1.size();mx=v[n-1];
    rep(i,0,x){
    	if(seg1[i]>=mx){
    		flg=0;
    		break;
    	}
    }
    if(flg)cout<<"YES\n";
    else cout<<"NO\n";
}

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


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;
typedef pair < int , int > par;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		if(arr[1] > arr[n]) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		while(1){
			vector < int > num;
			arr[n + 1] = 0;
			int tip = 0;
			int men = INT_MAX;
			for(int i = 2; i <= n + 1; i++){
				if(arr[i] > arr[i - 1]){
					men = min(men, arr[i - 1]);
					if(num.empty() && tip == 0)
						tip = arr[i - 1];
					else if(!num.empty() && tip == 0){
						if(arr[i - 1] > num.back())
							tip = arr[i - 1];
					}
					continue;
				}
				men = min(men, arr[i - 1]);
				if(num.empty() && tip == 0)
					tip = men;
				else if(!num.empty() && tip == 0){
					if(arr[i - 1] > num.back())
						tip = arr[i - 1];
					else
						tip = men;
				}
				num.push_back(tip);
				tip = 0;
				men = INT_MAX;
			}
			bool mark = false;
			for(int i = 0; i < num.size(); i++){
				arr[i + 1] = num[i];
				if(i > 0 && num[i] > num[i - 1])
					mark = true;
			}
			n = num.size();
			if(!mark) break;
		}
		if(n == 1) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;
typedef pair < int , int > par;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		stack < par > num;
		arr[n + 1] = 0;
		int cnt = 0;
		bool yes = true;
		int men = INT_MAX;
		for(int i = 2; i <= n + 1; i++){
			if(arr[i] >= arr[i - 1]){
				cnt++;
				continue;
			}
			if(men != INT_MAX && arr[i - 1] < men)
				yes = false;
			men = min(men, arr[i - 1 - cnt]);
		}
		if(yes) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;
typedef pair < int , int > par;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		stack < par > num;
		arr[n + 1] = 0;
		int cnt = 0;
		bool yes = true;
		int men = INT_MAX;
		for(int i = 2; i <= n + 1; i++){
			if(arr[i] >= arr[i - 1]){
				cnt++;
				continue;
			}
			if(men != INT_MAX && arr[i - 1] < men)
				yes = false;
			men = min(men, arr[i - 1 - cnt]);
			cnt = 0;
		}
		if(yes) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		vector < int > num;
		arr[n + 1] = 0;
		bool yes = true;
		int tip = 0;
		for(int i = 1; i <= n + 1; i++){
			if(i <= n) cin>>arr[i];
			if(i == 1) continue;
			if(arr[i] > arr[i - 1]){
				if(num.empty() && tip == 0)
					tip = arr[i - 1];
				else if(!num.empty() && tip == 0){
					if(arr[i - 1] > num.back())
						tip = arr[i - 1];
				}
				continue;
			}
			if(num.empty() && tip == 0)
				tip = arr[i - 1];
			else if(!num.empty() && tip == 0){
				if(arr[i - 1] > num.back())
					tip = arr[i - 1];
			}
			num.push_back(tip);
			tip = 0;
		}
		for(int i = 1; i < num.size(); i++){
			if(num[i] < num[i - 1])
				yes = false;
		}
		if(yes) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		while(1){
			vector < int > num;
			arr[n + 1] = 0;
			int tip = 0;
			int may = INT_MAX;
			for(int i = 2; i <= n + 1; i++){
				if(arr[i] > arr[i - 1]){
					if(may == INT_MAX && tip == 0)
						tip = arr[i - 1];
					else if(may != INT_MAX && tip == 0){
						if(arr[i - 1] > INT_MAX)
							tip = arr[i - 1];
					}
					continue;
				}
				if(tip == 0) tip = arr[i - 1];
				num.push_back(tip);
				may = min(may, tip);
				tip = 0;
			}
			bool mark = false;
			for(int i = 0; i < num.size(); i++){
				arr[i + 1] = num[i];
				if(i > 0 && num[i] > num[i - 1])
					mark = true;
			}
			n = num.size();
			if(!mark) break;
		}
		if(n == 1) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		vector < int > num;
		arr[n + 1] = 0;
		bool yes = true;
		int ant = 0;
		for(int i = 1; i <= n + 1; i++){
			if(i <= n) cin>>arr[i];
			if(i == 1) continue;
			if(arr[i] > arr[i - 1]){
				num.push_back(arr[i - 1]);
				continue;
			}
			num.push_back(arr[i - 1]);
			int y = upper_bound(num.begin(), num.end(), ant) - num.begin();
			if(y == num.size()) yes = false;
			else ant = num[y];
			num.clear();
		}
		if(yes) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		while(1){
			vector < int > num;
			arr[n + 1] = 0;
			int tip = 0;
			for(int i = 2; i <= n + 1; i++){
				if(arr[i] > arr[i - 1]){
					if(num.empty() && tip == 0)
						tip = arr[i - 1];
					else if(!num.empty() && tip == 0){
						if(arr[i - 1] > num.back())
							tip = arr[i - 1];
					}
					continue;
				}
				if(num.empty() && tip == 0)
					tip = arr[i - 1];
				else if(!num.empty() && tip == 0){
					tip = arr[i - 1];
				}
				num.push_back(tip);
				tip = 0;
			}
			bool mark = false;
			for(int i = 0; i < num.size(); i++){
				arr[i + 1] = num[i];
				if(i > 0 && num[i] > num[i - 1])
					mark = true;
			}
			n = num.size();
			if(!mark) break;
		}
		if(n == 1) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef long long ll;
typedef pair < int , int > par;

int const MAXn = 3e5 + 3;

int arr[MAXn];

int main(){

	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i = 1; i <= n; i++)
			cin>>arr[i];
		if(arr[1] < arr[n]) cout<<"YES";
		else cout<<"NO";
		cout<<"\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        int min_pos, max_pos;
        for0(i, n)
        {
            cin >> a[i];
            if(a[i] == 1) min_pos= i;
            else if(a[i] == n) max_pos  = i;
        };
        mini = a[0];
        int l = 0, r = n - 1;
        for0(i, n)
        {
            if(a[i] < mini) break;
            l = i;
            cnt++;
        }
        maxi = a[n - 1];
        for(int i = n - 1; i >= 0; i--)
        {
            if(a[i] > maxi) break;
            r = i;
            cnt++;
        }
        //cout << l << " " << r << endl;

        if((mini < maxi) && (r - l == 1 || r <= l))
        {
            cout << "YES\n";
            continue;
        }
        else if(maxi == n || mini == 1)
        {
            cout << "YES\n";
            continue;
        }
        mini= INT_MAX;
        maxi = -INT_MAX;
        for(int i = min_pos; i < n; i++)
        {
            maxi = max(a[i], (int)maxi);
        }
        for(int i = max_pos; i >= 0; i--)
        {
            mini = min(a[i], (int)mini);
        }
        //cout << maxi << " " << mini << endl;
        if(mini < maxi) cout << "YES\n";
        else cout << "NO\n";

    }

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        for(int i = 0; i < n; i++) cin >> a[i];
        if(a[0] < a[n - 1]) cout <<"YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        for0(i, n) cin >> a[i];
        mini = a[0];
        int l = 0, r = n - 1;
        for0(i, n)
        {
            if(a[i] < mini) break;
            l = i;
        }
        maxi = a[n - 1];
        for(int i = n - 1; i >= 0; i--)
        {
            if(a[i] > maxi) break;
            r = i;
        }
        //cout << l << " " << r << endl;

        if((mini < maxi) && (r - l == 1 || r <= l) ) cout << "YES\n";
        else if(maxi == n || mini == 1) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        for0(i, n) cin >> a[i];
        mini = a[0];
        int l = 0, r = n - 1;
        for0(i, n)
        {
            if(a[i] < mini) break;
            l = i;
        }
        maxi = a[n - 1];
        for(int i = n - 1; i >= 0; i--)
        {
            if(a[i] > maxi) break;
            r = i;
        }
        //cout << l << " " << r << endl;

        if((mini < maxi) && (r - l == 1 || r <= l) ) cout << "YES\n";
        else if(maxi == n) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        for0(i, n) cin >> a[i];
        mini = a[0];
        int l = 0, r = n - 1;
        for0(i, n)
        {
            if(a[i] < mini) break;
            l = i;
        }
        maxi = a[n - 1];
        for(int i = n - 1; i >= 0; i--)
        {
            if(a[i] > maxi) break;
            r = i;
        }
        //cout << l << " " << r << endl;
        if((mini < maxi) && (r - l == 1 || r <= l) ) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        int min_pos, max_pos;
        for0(i, n)
        {
            cin >> a[i];
            if(a[i] == 1) min_pos= i;
            else if(a[i] == n) max_pos  = i;
        }
        maxi = - INT_MAX;
        mini = INT_MAX;
        for(int i = min_pos; i < n; i++)
        {
            maxi = max(a[i], (int)maxi);
        }
        for(int i = max_pos; i >= 0; i--)
        {
            mini = min(a[i], (int)mini);
          //  cout << mini << endl;
        }
        //cout << mini << "=" << maxi << endl;
        if(mini < maxi) cout << "YES\n";
        else cout << "NO\n";

    }

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        int min_pos, max_pos;
        for0(i, n)
        {
            cin >> a[i];
            if(a[i] == 1) min_pos= i;
            else if(a[i] == n) max_pos  = i;
        }
        maxi = - INT_MAX;
        mini = INT_MAX;
        vint v(n);
        cnt = 0;
        for(int i = min_pos; i < n; i++)
        {
            maxi = max(a[i], (int)maxi);
            if(v[i] == 0) cnt++;
            v[i] = 1;

        }
        for(int i = max_pos; i >= 0; i--)
        {
            mini = min(a[i], (int)mini);
            if(v[i] == 0) cnt++;
            v[i] = 1;
          //  cout << mini << endl;
        }
        //cout << mini << "=" << maxi << endl;
        if(mini < maxi && cnt == n) cout << "YES\n";
        else cout << "NO\n";

    }

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll          map<ll,ll>
#define mstr        map<string,int>
#define si(a)        scanf("%day",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,b)     scanf("%d %d",&a, &b)
#define siii(a,b,c)  scanf("%d %d %d", &a, &b,&c)
#define siiii(a,b,c,d)  scanf("%d %d %d %d",&a, &b,&c,&d)
#define sl(a)        scanf("%lld",&a)
#define sll(a,b)     scanf("%lld %lld",&a, &b)
#define slll(a,b,c)  scanf("%lld %lld %lld",&a, &b, &c)
#define sllll(a,b,c,d)  scanf("%lld %lld %lld %lld",&a, &b, &c, &d)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfls(a)      printf("%lld",a)
#define pfii(a,b)    printf("%d %d\n",a,b)
#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,b)    printf("%lld %lld\n",a,b)
#define pflll(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define mem(a,b)     memset(a, b, sizeof(a));
#define max3(a,b,c)  max(max(a, b), c);
#define min3(a,b,c)  min(min(a, b), c);
#define min4(a,b,c,d)min(min(min(a, b), c), d);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const ll maxn = 9e9 + 5 , mod = 9e9;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
 ll  sum, cnt, tc,t, ans = 0, ans2,flag, maxi = -LLONG_MAX, mini = INT_MAX, pos, len;

string str;

int main()
{
   // fast;
    cin >> tc;
    while(tc--)
    {
        int n;
        cin >> n;
        int a[n];
        for(int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        stack<int> stk;
        stk.push(a[0]);
        for(int i = 1; i < n; i++)
        {
            int top = stk.top();
            if(top > a[i])
                stk.push(a[i]);
            else if(i == n - 1)
            {
                stk.pop();
                stk.push(a[i]);
            }
        }

        while(stk.size() > 1)
        {
           int top = stk.top();
           stk.pop();
           int left = stk.top();
           if(left < top)
           {
               stk.pop();
               stk.push(top);
           }
           else
           {
               stk.push(top);
               break;
           }
        }

        if(stk.size() == 1) cout << "YES\n";
        else cout << "NO\n";
    }

    return 0;
}


#include <bits/stdc++.h>

namespace {
    using namespace std;
#define int long long

    template<typename T>
    inline bool whitespace(const vector<T> &) { return false; }

    template<typename T>
    inline bool whitespace(const T &) { return true; }

    inline bool whitespace(const char) { return false; }

    template<typename F, typename S>
    inline bool whitespace(const pair<F, S> &) { return false; }

    template<typename F, typename S>
    ostream &operator<<(ostream &os, const pair<F, S> &p) {
        os << p.first << (whitespace(p.second) ? " " : "") << p.second << "\n";
        return os;
    }

    template<typename F, typename S>
    istream &operator>>(istream &is, pair<F, S> &p) {
        is >> p.first >> p.second;
        return is;
    }

    template<typename T>
    istream &operator>>(istream &is, vector<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const vector<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    template<typename T>
    istream &operator>>(istream &is, deque<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const deque<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    class exit_exception : exception {
    };

    void answer() {}

    template<typename First, typename... Args>
    void answer(First &&val, Args &&... ans) {
        cout << val << "\n";
        answer(ans...);
        throw exit_exception();
    }

    struct autoint {
        int x;

        autoint(int i) : x(i) {}

        autoint() : x(0) { cin >> x; }

        operator int() { return x; }

        int operator--() { return --x; }

        int operator--(signed) { return x--; }

        int operator++() { return ++x; }

        int operator++(signed) { return x++; }
    };

    template<typename T>
    T gcd(T a, T b) {
        while (b) {
            T mod = a % b;
            a = b;
            b = mod;
        }
        return a;
    }

    template<typename T>
    vector<T> prefix_sum(const vector<T> &v, int from = 0) {
        vector<T> res(v.size() + 1 - from);
        res[0] = 0;
        for (int i = from; i < v.size(); ++i) res[i + 1 - from] = res[i - from] + v[i];
        return res;
    }
}

void solve();

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    do { try { solve(); } catch (exit_exception &) {}} while (--t);
    return 0;
}

void solve() {
    autoint n;
    vector<int> a(n);
    cin >> a;
    vector<int> pmin = a, smax = a;
    for (int i = 1; i < n; ++i) {
        pmin[i] = min(pmin[i - 1], a[i]);
    }
    for (int i = n - 2; i >= 0; --i) {
        smax[i] = max(smax[i + 1], a[i]);
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] <= pmin[i] && a[i] >= smax[i]) answer("NO");
    }
    answer("YES");
}


#include <bits/stdc++.h>

namespace {
    using namespace std;
#define int long long

    template<typename T>
    inline bool whitespace(const vector<T> &) { return false; }

    template<typename T>
    inline bool whitespace(const T &) { return true; }

    inline bool whitespace(const char) { return false; }

    template<typename F, typename S>
    inline bool whitespace(const pair<F, S> &) { return false; }

    template<typename F, typename S>
    ostream &operator<<(ostream &os, const pair<F, S> &p) {
        os << p.first << (whitespace(p.second) ? " " : "") << p.second << "\n";
        return os;
    }

    template<typename F, typename S>
    istream &operator>>(istream &is, pair<F, S> &p) {
        is >> p.first >> p.second;
        return is;
    }

    template<typename T>
    istream &operator>>(istream &is, vector<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const vector<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    template<typename T>
    istream &operator>>(istream &is, deque<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const deque<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    class exit_exception : exception {
    };

    void answer() {}

    template<typename First, typename... Args>
    void answer(First &&val, Args &&... ans) {
        cout << val << "\n";
        answer(ans...);
        throw exit_exception();
    }

    struct autoint {
        int x;

        autoint(int i) : x(i) {}

        autoint() : x(0) { cin >> x; }

        operator int() { return x; }

        int operator--() { return --x; }

        int operator--(signed) { return x--; }

        int operator++() { return ++x; }

        int operator++(signed) { return x++; }
    };

    template<typename T>
    T gcd(T a, T b) {
        while (b) {
            T mod = a % b;
            a = b;
            b = mod;
        }
        return a;
    }

    template<typename T>
    vector<T> prefix_sum(const vector<T> &v, int from = 0) {
        vector<T> res(v.size() + 1 - from);
        res[0] = 0;
        for (int i = from; i < v.size(); ++i) res[i + 1 - from] = res[i - from] + v[i];
        return res;
    }
}

void solve();

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    do { try { solve(); } catch (exit_exception &) {}} while (--t);
    return 0;
}

void solve() {
    autoint n;
    vector<int> a(n);
    cin >> a;
    vector<pair<int, int>> p;
    for (int i = 0; i < n; ++i) {
        if (i == 0 || a[i] < a[i - 1]) p.emplace_back(a[i], 0);
        if (i == n - 1 || a[i] > a[i + 1]) p.back().second = a[i];
    }

    vector<int> pmin(p.size()), smax(p.size());
    pmin[0] = p[0].first;
    smax.back() = p.back().second;
    for (int i = 1; i < p.size(); ++i) {
        pmin[i] = min(pmin[i - 1], p[i].first);
    }
    for (int i = (int)p.size() - 2; i >= 0; --i) {
        smax[i] = max(smax[i + 1], p[i].second);
    }
    for (int i = 0; i < p.size(); ++i) {
        if (p[i].second <= pmin[i] && p[i].first >= smax[i]) answer("NO");
    }
    answer("YES");
}


#include <bits/stdc++.h>

namespace {
    using namespace std;
#define int long long

    template<typename T>
    inline bool whitespace(const vector<T> &) { return false; }

    template<typename T>
    inline bool whitespace(const T &) { return true; }

    inline bool whitespace(const char) { return false; }

    template<typename F, typename S>
    inline bool whitespace(const pair<F, S> &) { return false; }

    template<typename F, typename S>
    ostream &operator<<(ostream &os, const pair<F, S> &p) {
        os << p.first << (whitespace(p.second) ? " " : "") << p.second << "\n";
        return os;
    }

    template<typename F, typename S>
    istream &operator>>(istream &is, pair<F, S> &p) {
        is >> p.first >> p.second;
        return is;
    }

    template<typename T>
    istream &operator>>(istream &is, vector<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const vector<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    template<typename T>
    istream &operator>>(istream &is, deque<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const deque<T> &v) {
        for (const T &t : v) {
            os << t << (whitespace(t) ? " " : "");
        }
        os << "\n";
        return os;
    }

    class exit_exception : exception {
    };

    void answer() {}

    template<typename First, typename... Args>
    void answer(First &&val, Args &&... ans) {
        cout << val << "\n";
        answer(ans...);
        throw exit_exception();
    }

    struct autoint {
        int x;

        autoint(int i) : x(i) {}

        autoint() : x(0) { cin >> x; }

        operator int() { return x; }

        int operator--() { return --x; }

        int operator--(signed) { return x--; }

        int operator++() { return ++x; }

        int operator++(signed) { return x++; }
    };

    template<typename T>
    T gcd(T a, T b) {
        while (b) {
            T mod = a % b;
            a = b;
            b = mod;
        }
        return a;
    }

    template<typename T>
    vector<T> prefix_sum(const vector<T> &v, int from = 0) {
        vector<T> res(v.size() + 1 - from);
        res[0] = 0;
        for (int i = from; i < v.size(); ++i) res[i + 1 - from] = res[i - from] + v[i];
        return res;
    }
}

void solve();

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    do { try { solve(); } catch (exit_exception &) {}} while (--t);
    return 0;
}

void solve() {
    autoint n;
    vector<int> a(n);
    cin >> a;
    vector<pair<int, int>> p;
    for (int i = 0; i < n; ++i) {
        if (i == 0 || a[i] < a[i - 1]) p.emplace_back(a[i], 0);
        if (i == n - 1 || a[i] > a[i + 1]) p.back().second = a[i];
    }

    vector<int> pmin(p.size()), smax(p.size());
    pmin[0] = p[0].first;
    smax.back() = p.back().second;
    for (int i = 1; i < p.size(); ++i) {
        pmin[i] = min(pmin[i - 1], p[i].first);
    }
    for (int i = (int)p.size() - 2; i >= 0; --i) {
        smax[i] = max(smax[i + 1], p[i].second);
    }
    for (int i = 0; i + 1 < p.size(); ++i) {
        if (pmin[i] > smax[i + 1]) answer("NO");
    }
    answer("YES");
}


#include<bits/stdc++.h>
using namespace std ;
#define FAST         ios_base::sync_with_stdio(false);   cin.tie(NULL);
#define all(v) v.begin(),v.end()
#define nl         "\n"
typedef long long int  li;
#define BUFFER         cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define SORT        sort(a,a+n);
#define mod         1000000007
#define pb          push_back
#define test        ll t;cin>>t;while(t--)
#define FOR(i,x,n)   for(li i=x;i<n;i++)
#define fi first
#define se second
#define lb lower_bound
#define clear(a,z) memset(a,z,sizeof(a))
#define vp vector<pair<li,li> >
#define pr pair<li,li>
#define vi vector<li>
typedef long long ll;

bool minSwaps(int arr[], int n)
{
    // Create an array of pairs where first
    // element is array element and second element
    // is position of first element
    pair<int, int> arrPos[n];
    for (int i = 0; i < n; i++)
    {
        arrPos[i].first = arr[i];
        arrPos[i].second = i;
    }

    // Sort the array by array element values to
    // get right position of every element as second
    // element of pair.
    sort(arrPos, arrPos + n);

    int mv=arrPos[0].fi;

    bool flag=true;
    // To keep track of visited elements. Initialize
    // all elements as not visited or false.
    vector<bool> vis(n, false);

    // Initialize result
    int ans = 0;

    // Traverse array elements
    for (int i = 0; i < n; i++)
    {
        // already swapped and corrected or
        // already present at correct pos
        if (vis[i] || arrPos[i].second == i)
            continue;

        // find out the number of  node in
        // this cycle and add in ans
        int cycle_size = 0;
        int j = i;
        while (!vis[j])
        {
            vis[j] = 1;
            if(__gcd(arr[j],arr[arrPos[j].se])!=mv && arr[j]!=arr[arrPos[j].se]) flag=false;
            // move to next node
            j = arrPos[j].second;
            cycle_size++;
        }

    }

    // Return result
    return flag;
}

int main(){

    #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
    #endif

    FAST
    test{

      int n;
      cin>>n;
      int arr[n];
       FOR(i,0,n) cin>>arr[i];

       if(minSwaps(arr,n)) cout<<"YES"<<endl;
       else cout<<"NO"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std ;
#define FAST         ios_base::sync_with_stdio(false);   cin.tie(NULL);
#define all(v) v.begin(),v.end()
#define nl         "\n"
typedef long long int  li;
#define BUFFER         cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define SORT        sort(a,a+n);
#define mod         1000000007
#define pb          push_back
#define test        ll t;cin>>t;while(t--)
#define FOR(i,x,n)   for(li i=x;i<n;i++)
#define fi first
#define se second
#define lb lower_bound
#define clear(a,z) memset(a,z,sizeof(a))
#define vp vector<pair<li,li> >
#define pr pair<li,li>
#define vi vector<li>
typedef long long ll;

bool compre(pr i1,pr i2){
    if(i1.fi==i2.fi) return i1.se>i2.se;
    return i1.fi<i2.fi;
}

bool minSwaps(int arr[], int n)
{
    // Create an array of pairs where first
    // element is array element and second element
    // is position of first element
    pair<int, int> arr2[n];
    for (int i = 0; i < n; i++)
    {
        arr2[i].first = arr[i];
        arr2[i].second = i;
    }

    // Sort the array by array element values to
    // get right position of every element as second
    // element of pair.
    sort(arr2, arr2 + n,compre);

    int mv=arr2[0].fi;

    bool flag=true;
    // To keep track of visited elements. Initialize
    // all elements as not visited or false.
    vector<bool> vis(n, false);

    // Initialize result
    int ans = 0;

    // Traverse array elements
    for (int i = 0; i < n; i++)
    {
        // already swapped and corrected or
        // already present at correct pos
        if (arr2[i].second == i)
            continue;

          if(arr2[i].fi%mv!=0) flag=false;

    }

    // Return result
    return flag;
}

int main(){

    #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
    #endif

    FAST
    test{

      int n;
      cin>>n;
      int arr[n];
       FOR(i,0,n) cin>>arr[i];

       if(minSwaps(arr,n)) cout<<"YES"<<endl;
       else cout<<"NO"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std ;
#define FAST         ios_base::sync_with_stdio(false);   cin.tie(NULL);
#define all(v) v.begin(),v.end()
#define nl         "\n"
typedef long long int  li;
#define BUFFER         cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define SORT        sort(a,a+n);
#define mod         1000000007
#define pb          push_back
#define test        ll t;cin>>t;while(t--)
#define FOR(i,x,n)   for(li i=x;i<n;i++)
#define fi first
#define se second
#define lb lower_bound
#define clear(a,z) memset(a,z,sizeof(a))
#define vp vector<pair<li,li> >
#define pr pair<li,li>
#define vi vector<li>
typedef long long ll;

bool compre(pr i1,pr i2){
    // if(i1.fi==i2.fi) return i1.se>i2.se;
    return i1.fi<i2.fi;
}

bool minSwaps(int arr[], int n)
{
    // Create an array of pairs where first
    // element is array element and second element
    // is position of first element
    pair<int, int> arr2[n];
    for (int i = 0; i < n; i++)
    {
        arr2[i].first = arr[i];
        arr2[i].second = i;
    }

    // Sort the array by array element values to
    // get right position of every element as second
    // element of pair.
    sort(arr2, arr2 + n,compre);

    int mv=arr2[0].fi;

    bool flag=true;
    // To keep track of visited elements. Initialize
    // all elements as not visited or false.
    vector<bool> vis(n, false);

    // Initialize result
    int ans = 0;

    // Traverse array elements
    for (int i = 0; i < n; i++)
    {
        // already swapped and corrected or
        // already present at correct pos
        if (arr2[i].second == i || arr2[i].fi==arr2[arr2[i].se].fi)
            continue;

          if(arr2[i].fi%mv!=0) flag=false;

    }

    // Return result
    return flag;
}

int main(){

    #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
    #endif

    FAST
    test{

      int n;
      cin>>n;
      int arr[n];
       FOR(i,0,n) cin>>arr[i];

       if(minSwaps(arr,n)) cout<<"YES"<<endl;
       else cout<<"NO"<<endl;

    }
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[100005];
ll b[100005];
vector<ll> v;

bool solve() {
    v.clear();
    int n = readint();
    ll Min = inf;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (v.empty()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1ll) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

int a[100005];
vector<int> v;

bool solve() {
    v.clear();
    int n = readint();
    int Min = INF;
    for (int i = 0; i < n; i++) a[i] = readint(), Min = min(Min, a[i]);
    for (int i = 0; i < n; i++) if (a[i] % Min) v.push_back(a[i]);
    for (int i = 1; i < v.size(); i++) if (v[i] < v[i - 1]) return false;
    if (v.size() == 1) {
        for (int i = 1; i < n; i++) if (a[i] < a[i - 1]) return false;
        return true;
    }
    return true;

}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[200005];
ll b[200005];

bool solve() {
    vector<ll> v;
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (!v.size()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    return g == 1ll;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[200005];
ll b[200005];
vector<ll> v;

bool solve() {
    v.clear();
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (!v.size()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1ll) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

int a[100005];
vector<int> v;

bool solve() {
    v.clear();
    int n = readint();
    int Min = INF;
    for (int i = 0; i < n; i++) a[i] = readint(), Min = min(Min, a[i]);
    for (int i = 0; i < n; i++) if (a[i] % Min) v.push_back(a[i]);
    for (int i = 1; i < v.size(); i++) if (v[i] < v[i - 1]) return false;
    return true;

}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[maxn];
ll b[maxn];

bool solve() {
    vector<ll> v;
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(), b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min != 0) return false;
        }
    }
    return true;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[100005];
ll b[100005];
vector<ll> v;

bool solve() {
    v.clear();
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (v.empty()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1ll) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[100005];
ll b[100005];
vector<ll> v;

bool solve() {
    v.clear();
    int n = readint();
    ll Min = 1e18;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (!v.size()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1ll) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

ll a[100005];
ll b[100005];
vector<int> v;

bool solve() {
    v.clear();
    int n = readint();
    ll Min = inf;
    for (int i = 0; i < n; i++) a[i] = readll(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (v.empty()) return true;
    ll g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1ll) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}


#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F
#define inf 998244353
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-6
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 1e5 + 10;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;

int bitcount(int x) {
    return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}

ll mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res % m;
}

ll quickPower(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans = mul(ans, base, m);
        base = mul(base, base, m);
        b >>= 1;
    }
    return ans;
}

ll ksm(ll a, ll b, ll m) {
    ll base = a;
    ll ans = 1ll;
    while (b) {
        if (b & 1) ans *= base, ans %= m;
        base *= base, base %= m;
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll Lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

int readint() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ll readll() {
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

ull readull() {
    ull x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

void Put(ll x) {
    if (x < 0) putchar('-'), x *= -1;
    if (x > 9) Put(x / 10);
    putchar(x % 10 + '0');
}

int a[100005];
int b[100005];
vector<int> v;

bool solve() {
    v.clear();
    int n = readint();
    int Min = INF;
    for (int i = 0; i < n; i++) a[i] = readint(),b[i] = a[i], Min = min(Min, a[i]);
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            if (a[i] % Min) return false;
            v.push_back(a[i] / Min);
        }
    }
    if (v.empty()) return true;
    int g = v[0];
    for (int i = 1; i < v.size(); i++) g = gcd(g, v[i]);
    if (g == 1) return true;
    return false;
}

int main() {
    int T = readint();
    while (T--) if (solve()) puts("YES");
    else puts("NO");
}