#include <bits/stdc++.h>

using namespace std;

#define flush cout.flush

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);

void solve() {
    ll n;
    cin >> n;
    vector<ll> b(n);
    for (ll &i : b)cin >> i;
    vector<ll> before, after;
    ll cur = b[0];
    before.push_back(cur);
    for (ll i = 1; i < n; ++i) {
        if (b[i] > cur) {
            cur = b[i];
            if (after.empty()) {
                before.push_back(cur);
                continue;
            }
            ll x = after.back();
            if (x < cur) {
                cout << "NO\n";
                return;
            }
            if (x == cur) {
                after.pop_back();
                before.push_back(cur);
                continue;
            }
            before.push_back(cur);
        } else if (b[i] < cur) {
            cur = b[i];
            if (before.size() == 1) {
                ll a = before[0];
                before.pop_back();
                before.push_back(cur);
                after.push_back(a);
                continue;
            }
            ll x = before[before.size() - 2];
            if (x > cur) {
                cout << "NO\n";
                return;
            }
            if (x == cur) {
                ll a = before.back();
                before.pop_back();
                after.push_back(a);
                continue;
            }
            ll a = before.back();
            before.pop_back();
            after.push_back(a);
            before.push_back(cur);
        }
    }
    cout << "YES\n";

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while (t--)solve();
    return 0;
}