#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back

typedef pair<int,int> pi;

const int N = 500011;
const int NEGINF = -1000000011;

int A[N];
int nxt[N];
vector<pi> to_update[N];

/* ----- SEGMENT TREE ----- */
pi st[N << 2];

void build(int p, int l, int r) {
	if(l == r) st[p] = pi(l, NEGINF);
	else {
		int m = (l + r) >> 1;
		int P = p << 1;
		build(P,l,m);
		build(P|1,m+1,r);
		st[p] = st[P];
	}
}

pi combine(pi a, pi b) {
	if(a.se >= b.se) return a;
	return b;
}

void update(int p, int l, int r, int k, int v) {
	if(l == r) st[p].se = v;
	else {
		int m = (l + r) >> 1;
		int P = p << 1;
		if(k <= m) update(P,l,m,k,v);
		else update(P|1,m+1,r,k,v);
		st[p] = combine(st[P], st[P|1]);
	}
}

pi query(int p, int l, int r, int mx) {
	if(l > mx) return pi(-1, NEGINF);
	if(r <= mx) return st[p];
	int m = (l + r) >> 1;
	int P = p << 1;
	return combine(query(P,l,m,mx), query(P|1,m+1,r,mx));
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;
	while(t--) {

		int n;
		cin >> n;
		A[0] = 0;
		for(int i=0; i<n; i++) cin >> A[i+1];
		vector<int> tmp;
		for(int i=n; i>=0; i--) {
			nxt[i] = -1;
			int lq = 0, rq = (int)tmp.size() - 1;
			while(lq <= rq) {
				int mq = (lq + rq) >> 1;
				if(A[tmp[mq]] >= A[i]) {
					nxt[i] = tmp[mq];
					lq = mq+1;
				}
				else rq = mq-1;
			}
			while(!tmp.empty() and A[i] >= A[tmp.back()]) 
				tmp.pop_back();
			tmp.push_back(i);
		}

		build(1, 0, n);
		int ans = 0;
		update(1, 0, n, 0, 0);

		for(int i=0; i<=n; i++) {
			auto que = query(1, 0, n, A[i]);
			int idx = que.fi, val = que.se + 1;
			ans = max(ans, val);
			update(1, 0, n, A[i], val);
			while(!to_update[i].empty()) {
				auto cur = to_update[i].back();
				to_update[i].pop_back();
				update(1, 0, n, cur.fi, cur.se);
				ans = max(ans, cur.se);
			}
			if(nxt[i] != -1) to_update[nxt[i]].eb(A[i], val+1);
		}

		cout << ans-1 << endl;

	}

	return 0;
}