#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using dt = array<int, 2>;
bool ok(deque<int> in, deque<int> out) {
while (out.size()) {
if (out.front() == out.back()) {
if (out.front() >= in.back()) {
in.push_back(out.front());
out.pop_front();
} else if (out.front() <= in.front()) {
in.push_front(out.front());
out.pop_front();
} else {
return false;
}
} else if (out.front() < out.back()) {
if (out.front() >= in.back()) {
in.push_back(out.front());
out.pop_front();
continue;
}
if (out.back() <= in.front()) {
in.push_front(out.back());
out.pop_back();
continue;
}
if (out.front() <= in.front()) {
in.push_front(out.front());
out.pop_front();
continue;
}
if (out.back() >= in.back()) {
in.push_back(out.back());
out.pop_back();
continue;
}
return false;
} else {
if (out.back() >= in.back()) {
in.push_back(out.back());
out.pop_back();
continue;
}
if (out.front() <= in.front()) {
in.push_front(out.front());
out.pop_front();
continue;
}
if (out.back() <= in.front()) {
in.push_front(out.back());
out.pop_back();
continue;
}
if (out.front() >= in.back()) {
in.push_back(out.front());
out.pop_front();
continue;
}
return false;
}
}
// for (auto e : in) cout << e << " ";
// cout << "\n";
return true;
}
void test() {
int n;
cin >> n;
deque<int> v;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
auto a = v;
deque<int> final;
final.push_back(v.front());
v.pop_front();
if (ok(final, v)) {
cout << "YES\n";
return;
}
v = a;
final.clear();
final.push_back(v.back());
v.pop_back();
if (ok(final, v)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++) {
test();
}
return 0;
}