#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
void write(int x){
	if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeln(int x){
	write(x);puts("");
}
const int N=5e5+10;
int rt[N],ts[20*N],ls[20*N],rs[20*N],cnt=0;
int f[N],Cnt;
int n,a[N],c[N],la[N],dp[N];

inline void fadd(int x,int v){
	for(;x<=Cnt;x+=x&-x)f[x]=max(f[x],v);
}
inline int fmm(int x){
	x--;int ans=0;
	for(;x;x-=x&-x)ans=max(ans,f[x]);
	return ans;
}
inline void Xg(int l,int r,int i,int v,int &nod,int la){
	nod=++cnt,ts[nod]=ts[la],ls[nod]=ls[la],rs[nod]=rs[la];
	if(l==r){
		ts[nod]=max(ts[nod],v);
		return;
	}
	int mid=l+r>>1;
	if(i<=mid)Xg(l,mid,i,v,ls[nod],ls[la]);
	else Xg(mid+1,r,i,v,rs[nod],rs[la]);
	ts[nod]=max(ts[ls[nod]],ts[rs[nod]]);
}

inline int Smax(int l,int r,int i,int j,int nod){
	if(l>r||(!nod))return 0;
	if(l>=i&&r<=j)return ts[nod];
	int mid=l+r>>1,ans=0;
	if(i<=mid)ans=max(ans,Smax(l,mid,i,j,ls[nod]));
	if(j>mid)ans=max(ans,Smax(mid+1,r,i,j,rs[nod]));
	return ans;
}
inline void pre(){
	for(int i=1;i<=n;i++)f[i]=dp[i]=0;
	for(int i=0;i<=Cnt;i++)rt[i]=0;
	for(int i=0;i<=cnt;i++)ts[i]=ls[i]=rs[i]=0;
	cnt=0;
	Xg(0,Cnt,0,0,rt[0],rt[0]);
}

int main()
{
	for(int T=read();T;T--){
		n=read();Cnt=0;
		for(int i=1;i<=n;i++)a[i]=c[i]=read();
		sort(c+1,c+n+1);Cnt=unique(c+1,c+n+1)-c-1;
		for(int i=1;i<=n;i++)a[i]=lower_bound(c+1,c+Cnt+1,a[i])-c;
		pre();
		for(int i=1;i<=n;i++){
			la[i]=fmm(Cnt-a[i]+1);
			fadd(Cnt-a[i]+1,i);
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			f[i]=Smax(0,Cnt,0,a[i],rt[i-1])+1;
			if(la[i])f[i]=max(f[i],Smax(0,Cnt,0,a[i],rt[la[i]-1])+2);
			ans=max(ans,f[i]);
//			cout<<i<<' '<<a[i]<<' '<<la[i]<<' '<<f[i]<<' '<<Smax(0,Cnt,0,c[i],rt[la[i]-1])+2<<endl;
			Xg(0,Cnt,a[i],f[i],rt[i],rt[i-1]);
		}
		writeln(ans);
	}
	return 0;
}