#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 500005
#define M 100005
#else
#define fast 
#define N 105
#define M 105
#endif
#define forn(i, n) for(int i = 0; i < (n); i++)
#define forab(i, a, b) for(int i = (a); i <= (b); i++)
#define forba(i, b, a) for(int i = (b); i >= (a); i--)
#define INF 0x3f3f3f3f
#define mk(x) (1<<(x)) // be conscious if mask x exceeds int
#define sz(x) ((int)x.size())
#define upperdiv(a,b) (a/b + (a%b>0))
#define mp(a,b) make_pair(a, b)
#define endl '\n'
#define lowbit(x) (x&-x)

using namespace std;
typedef long long ll;
typedef double db;

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

/** fast read **/
template <typename T>
inline void read(T &x) {
    x = 0; T fg = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') fg = -1;ch = getchar();}
    while (isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    x = fg * x;
}
template <typename T, typename... Args>
inline void read(T &x, Args &... args) { read(x), read(args...); }
template <typename T>
inline void write(T x) {
    int len = 0; char c[21]; if (x < 0) putchar('-'), x = -x;
    do{++len; c[len] = x%10 + '0';} while (x /= 10);
    for (int i = len; i >= 1; i--) putchar(c[i]);
}
template <typename T, typename... Args>
inline void write(T x, Args ... args) { write(x), write(args...); }

int n;
int a[N];

int ft[N];
void add(int u, int v) {
    u++;
    for (; u <= n+1; u += lowbit(u))
        ckmax(ft[u], v);
}
int ask(int u) {
    int res = 0;
    u++;
    for (; u; u -= lowbit(u))
        ckmax(res, ft[u]);
    return res;
}

int nxt[N];
int s[N];
int d[N];
vector<int> pres[N];
int main() {
    int t; read(t);
    while (t--) {
        read(n);
        forab(i, 1, n) read(a[i]);
        memset(nxt, -1, (n + 1) * sizeof(int));
        forab(i, 0, n+1) {
            pres[i].clear();
        }
        s[0] = 0;
        forab(i, 0, n) {
            while (s[0] > 0 && a[s[s[0]]] <= a[i]) {
                nxt[s[s[0]]] = i;
                s[0]--;
            }
            s[++s[0]] = i;
        }

        memset(ft, 0, (n+2) * sizeof(int));
        forab(i, 0, n) {
            d[i] = ask(a[i]) + 1;
            add(a[i], d[i]);
            if (nxt[i] != -1)
                pres[nxt[i]].push_back(i);
            for(int j : pres[i])
                add(a[j], d[j] + 1);
        }
        int ans = ask(n) - 1;
        cout << ans << endl;
    }
    return 0;
}
/*
3
8
1 2 7 3 2 1 2 3
2
2 1
7
4 1 5 2 6 3 7

1
500
169
291
471
404
199
306
21
289
147
223
109
97
100
477
188
485
454
170
95
429
398
385
397
12
53
207
107
163
417
418
487
94
335
53
286
69
207
358
268
285
368
54
420
33
213
357
282
453
141
16
479
439
272
404
84
109
452
466
107
155
313
205
364
395
76
105
18
300
184
24
364
471
132
148
88
223
387
343
239
320
101
75
335
491
359
498
444
464
398
18
451
302
36
382
418
279
158
260
326
238
297
333
483
419
373
252
*/