#include<bits/stdc++.h>
using namespace std;
//ofstream file("output0.txt");
void solve()
{
int n,x; cin>>n;
vector<int>one,two,three;
for(int i=0;i<n;i++)
{
cin>>x;
if(x==1) one.push_back(i);
else if(x==2) two.push_back(i);
else if(x==3) three.push_back(i);
}
int ans = 0;
int sz = one.size();
int lo,hi,mid,ind1,ind2;
for(auto e:one)
{
lo=0,hi=two.size()-1,ind1=n;
while(lo<=hi)
{
mid=(lo+hi)/2;
if(two[mid]>e)
{
ind1 = two[mid];
hi=mid-1;
}
else lo=mid+1;
}
lo=0,hi=three.size()-1,ind2=-1;
while(lo<=hi)
{
mid=(lo+hi)/2;
if(three[mid]<ind1)
{
ind2 = three[mid];
lo=mid+1;
}
else hi=mid-1;
}
ans = max(ans,ind2-e+1);
}
cout<<ans<<endl;
//file<<ans<<endl;
}
int main()
{
//freopen("input0.txt","r",stdin);
int t; cin>>t; while(t--) solve();
}