#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector<ll> v(n + 2), tmp = {0};
set<ll> st;
for(int i = 1; i <= n; i++) {
cin >> v[i];
// st.insert(v[i]);
}
for(int i = 1; i <= n; ) {
int j = i + 1;
while(j <= n && v[i] == v[j]) ++j;
tmp.push_back(v[i]);
st.insert(v[i]);
i = j;
}
v = tmp;
n = int(v.size()) - 1;
if(n <= 3) {
cout << "YES" << endl;
return;
}
bool ok = 1;
ll l = 2, r = n, mn = v[1], mx = v[1];
while(l <= r) {
// cout << l << " " << r << endl;
// for l
if(!(mn < v[l] && v[l] < mx)) {
if(v[l] == mn or v[l] == mx) {
++l;
continue;
}
if(v[l] < mn) {
auto it = st.upper_bound(v[l]);
if(it != st.end() && *it == mn) {
mn = v[l];
++l;
continue;
}
}
else if(mx < v[l]) {
auto it = st.upper_bound(mx);
if(it != st.end() && *it == v[l]) {
mx = v[l];
++l;
continue;
}
}
}
if(l > r) break;
// for r
if(!(mn < v[r] && v[r] < mx)) {
if(v[r] == mn or v[r] == mx) {
--r;
continue;
}
if(v[r] < mn) {
auto it = st.upper_bound(v[r]);
if(it != st.end() && *it == mn) {
mn = v[r];
--r;
continue;
}
}
else if(mx < v[r]) {
auto it = st.upper_bound(mx);
if(it != st.end() && *it == v[r]) {
mx = v[r];
--r;
continue;
}
}
}
// t << l << " " << r << endl;
ok = 0;
break;
}
if(ok) {
cout << "YES" << endl;
return;
}
ok = 1;
l = 1, r = n - 1, mn = v[n], mx = v[n];
while(l <= r) {
// for l
if(!(mn < v[l] && v[l] < mx)) {
if(v[l] == mn or v[l] == mx) {
++l;
continue;
}
if(v[l] < mn) {
auto it = st.upper_bound(v[l]);
if(it != st.end() && *it == mn) {
mn = v[l];
++l;
continue;
}
}
else if(mx < v[l]) {
auto it = st.upper_bound(mx);
if(it != st.end() && *it == v[l]) {
mx = v[l];
++l;
continue;
}
}
}
if(l > r) break;
// for r
if(!(mn < v[r] && v[r] < mx)) {
if(v[r] == mn or v[r] == mx) {
--r;
continue;
}
if(v[r] < mn) {
auto it = st.upper_bound(v[r]);
if(it != st.end() && *it == mn) {
mn = v[r];
--r;
continue;
}
}
else if(mx < v[r]) {
auto it = st.upper_bound(mx);
if(it != st.end() && *it == v[r]) {
mx = v[r];
--r;
continue;
}
}
}
// cout << l <<" " << r << endl;
ok = 0;
break;
}
if(ok) {
cout << "YES" << endl;
return;
}
cout << "NO" << endl;
return;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}