#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
deque<int> temp1, temp2;
bool solve(int pos){
int l = pos, r = pos;
while (!temp2.empty()){
int first = temp2.front();
int second = temp2.back();
if (l > 0 && (a[l - 1] == first or a[l - 1] == second)){
l--;
if (a[l] == first) temp2.pop_front();
else temp2.pop_back();
} else if (r + 1 < n && (a[r + 1] == first or a[r + 1] == second)){
r++;
if (a[r] == first) temp2.pop_front();
else temp2.pop_back();
} else {
return false;
}
}
return true;
}
int main() {
int t;
cin >> t;
while (t--) {
bool ok = false;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
temp1.push_back(A[i]);
}
temp2 = temp1;
a = A;
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
if (a[i] == temp2.front()) {
temp2.pop_front();
ok = solve(i);
break;
}
}
int y=0;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
y++;
}
}
while (!temp2.empty()) temp2.pop_back();
temp2 = temp1;
for (int i = 0; i < n; i++) {
if (a[i] == temp2.back()) {
temp2.pop_back();
ok = ok or solve(i);
break;
}
}
cout << (ok ? "YES " : "NO") << endl;
while (!temp2.empty()) temp2.pop_back();
while (!temp1.empty()) temp1.pop_back();
}
}