#include<stdio.h>
#define NMAX 10001
long yp,poz,i,j,n,m,k,l,q,a,s,x[NMAX],y[NMAX],z;
int kkt(long poz,long y[NMAX],long yp)
{
if ((poz>n&&yp<=y[0])||(poz<=n&&yp>y[0])) return 0;
if (yp>y[0]&&poz>n) {z=1;return 1;}
if (z==1) return 1;
if (yp<=y[0])
{
if (y[yp]==1)
if (x[poz]==1||x[poz]==2||x[poz]==3)
kkt(poz+1,y,yp+1);
if (y[yp]==2)
{
if (x[poz]==2)
kkt(poz+1,y,yp);
if (x[poz]==1&&x[poz+2]==3&&poz+4<=n)
{
y[yp]=3;
kkt(poz+4,y,yp);
}
}
if (y[yp]==3)
{
if (x[poz]==2)
kkt(poz+1,y,yp+1);
if (x[poz]==1&&x[poz+1]==2&&poz+2<=n)
kkt(poz+3,y,yp+1);
if (x[poz]==3)
{
y[++y[0]]=3;
y[yp]=2;
kkt(poz+1,y,yp);
}
}
}
else
{
if (x[poz]==1||x[poz]==2||x[poz]==3)
kkt(poz+1,y,yp+1);
if (x[poz]==2)
kkt(poz+1,y,yp);
if (x[poz]==1&&x[poz+2]==3&&poz+4<=n)
{
y[yp]=3;
kkt(poz+4,y,yp);
}
if (x[poz]==2)
kkt(poz+1,y,yp+1);
if (x[poz]==1&&x[poz+1]==2&&poz+3<=n)
kkt(poz+1,y,yp+1);
if (x[poz]==3)
{
y[++y[0]]=3;
y[yp]=2;
kkt(poz+1,y,yp);
}
}
if (z==0)return 0;
return 1;
}
int main()
{
freopen("perle.in","r",stdin);
freopen("perle.out","w",stdout);
scanf("%ld",&m);
for (l=1;l<=m;l++)
{
scanf("%ld",&n);
for (i=1;i<=n;i++)
scanf("%ld",&x[i]);
y[0]=1;
z=0;
yp=0;
a=0;
poz=1;
if (x[1]==1)
{
y[1]=1;
yp=1;
q=kkt(poz+1,y,yp);
if (q) {printf("1\n"); a=1;}
if (a==0)
{
y[1]=2;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
}
if (a==0)
{
y[1]=3;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
}
}
if (x[1]==2&&a==0)
{
y[1]=1;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
if (a==0)
{
y[1]=2;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
}
if (a==0)
{
y[1]=3;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
}
}
if (x[1]==3&&a==0)
{
y[1]=1;
yp=1;
q=kkt(poz,y,yp);
if (q) {printf("1\n"); a=1;}
if (a==0)
{
y[1]=2;
y[2]=3;
y[0]=2;
yp=1;
q=kkt(poz+1,y,yp);
if (q) {printf("1\n"); a=1;}
}
}
// q=kkt(poz,y,yp);
// if (a==1) printf("1\n");
if (a==0)
printf("0\n");
}
return 0;
}