#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin(“perle.in”);
ofstream fout (“perle.out”);
int n;
vector <int> v;
bool Perla( char c)
{
stack<char> stk;
int p = 1;
char l;
stk.push( c );
while( p <= n )
{
if( stk.empty()) return false;
l = stk.top();
stk.pop();
if( l 'B')
{
if( v[p] 2)
{
stk.push(‘B’);
p++;
continue;
}
if( p + 4 <= n && v[p] 1 && v[p + 2] 3)
{
stk.push(‘C’);
p = p + 4;
continue;
}
if( v[p] 3 ) return false;
}
if( l ‘C’)
{//C -> 2 | 3BC | 12A
if( v[p] 2 )
{
p++;
continue;
}
if( v[p] 3 )
{
stk.push(‘C’);
stk.push(‘B’);
p++;
continue;
}
if(p + 2 <= n && v[p] 1 && v[p + 1] 2 )
{
p = p + 3;
continue;
}
}
}
return stk.empty();
}
int main()
{
int m;
fin >> m;
for(int i = 1; i <= m; i++)
{
fin >> n;
v = vector<int>( n + 1);
for(int j = 1; j <= n; j++)
fin >> v[j];
if( Perla(‘B’) true || Perla ('C') true || n == 1)
fout << “1\n”;
else
fout << “0\n”;
}
}