#include <bits/stdc++.h>
#define ll long long
#define int long long
#define FIO ios::sync_with_stdio(false);cin.tie(NULL)
#define debug(x) cout<<#x<<' '<<x<<endl;
#define const char endl '/n'
#define all(x) x.begin(),x.end()
using namespace std;
void NO(){cout<<"NO"<<endl;}
void YES(){cout<<"YES"<<endl;}
bool isprime(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
return 0;
}
}
return true;
}
bool cmp(int &a,int &b){
if(b<a){
return true;
}else{
return false;
}
}
void solve(){
int n;cin>>n;
int arr[n];
vector<int> v;
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]&&arr[i]>=10 ){
int p=arr[i];
int a=p%10;
p/=10;
v.push_back(p);
v.push_back(a);
}else{
v.push_back(arr[i]);
}
}
v.push_back(arr[n-1]);
// for(auto &it:v){
// cout<<it<<' ';
// }
// cout<<endl;
if(is_sorted(v.begin(),v.end())){
YES();
}else{
NO();
}
}
int32_t main(){
FIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// #ifndef ONLINE_JUDGE
// freopen("whereami.in", "r", stdin);
// freopen("whereami.out", "w", stdout);
// #endif
int t=1;cin>>t;while(t--)
solve();
}
ICAgICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBGSU8gaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpO2Npbi50aWUoTlVMTCkKI2RlZmluZSBkZWJ1Zyh4KSBjb3V0PDwjeDw8JyAnPDx4PDxlbmRsOwojZGVmaW5lIGNvbnN0IGNoYXIgZW5kbCAnL24nCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSx4LmVuZCgpCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp2b2lkIE5PKCl7Y291dDw8Ik5PIjw8ZW5kbDt9CnZvaWQg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