#include<bits/stdc++.h>
#define zmrl(x) signed((x).size())
#define ahen(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using pii = pair<int,int>;

struct node {
    int le, ri, val;
};

int n;
int root[500500];
vector<node> tree;

// nidx : 노드 번호, 구간 st~en
void init(int nidx, int st, int en) {
    if (st==en) return;
    tree.pb({0,0,0});
    tree[nidx].le=zmrl(tree)-1;
    init(tree[nidx].le, st, st+en>>1);
    tree.pb({0,0,0});
    tree[nidx].ri=zmrl(tree)-1;
    init(tree[nidx].ri, (st+en>>1)+1, en);
}

// uidx : 갱신하는 인덱스 번호
void update(int nidx, int st, int en, int uidx, int val) {
    if (st==en) return;
    int mid=st+en>>1;
    if (uidx<=mid) {
        int lidx=tree[nidx].le;
        tree.pb({tree[lidx].le, tree[lidx].ri, max(tree[lidx].val, val)});
        update(tree[nidx].le=zmrl(tree)-1, st, mid, uidx, val);
    }
    else {
        int ridx=tree[nidx].ri;
        tree.pb({tree[ridx].le, tree[ridx].ri, max(tree[ridx].val, val)});
        update(tree[nidx].ri=zmrl(tree)-1, mid+1, en, uidx, val);
    }
}

int solve(int nidx, int st, int en, int le, int ri) {
    if (ri < st || le > en || le > ri) return 0;
    if (le <= st && en <= ri) return tree[nidx].val;
    return max(solve(tree[nidx].le, st, st+en>>1, le, ri), solve(tree[nidx].ri, (st+en>>1)+1, en, le, ri));
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int TC; cin>>TC;
    int A[500500];
    for (int tc=1; tc<=TC; tc++) {
        cin>>n;
        for (int i=1; i<=n; i++) cin>>A[i];
        tree.clear();
        tree.pb({0,0,0});
        init(0, 1, n);
        stack<int> st;
        int res = 0;
        for (int i=n; i>=1; i--) {
            res = max(res, solve(root[n-i], 1, n, 1, A[i]-1) + 1);
            while (zmrl(st) && A[st.top()] < A[i]) st.pop();
            int mmax = solve(root[n-i], 1, n, A[i], n) + 1;
            if (zmrl(st)) {
                int idx = st.top();
                mmax = max(mmax, solve(root[n-idx], 1, n, A[i], A[idx]-1) + 2);
            }
            st.push(i);
            tree.pb({tree[root[n-i]].le, tree[root[n-i]].ri, tree[root[n-i]].val});
            root[n+1-i] = zmrl(tree)-1;
            tree[root[n+1-i]].val = max(tree[root[n+1-i]].val, mmax);
            update(root[n+1-i], 1, n, A[i], mmax);
        }
        cout<<max(res, solve(root[n], 1, n, 1, n))<<'\n';
    }
    return 0;
}