#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int main() {
	int n=in();
	int a[10];
	string b[10];
	while(n--){
		int i;
		for(i=0;i<9;i++){
			cin>>a[i];
		}
		for(i=0;i<9;i++){
			cin>>b[i];
		}
		int per[]={0,1,2,3,4,5,6,7,8};
		bool win=false;
		do{
//cout<<per[0]<<" "<<per[1]<<" "<<per[2]<<" "<<per[3]<<" "<<per[4]<<" "<<per[5]<<" "<<per[6]<<" "<<per[7]<<" "<<per[8]<<endl;
			int set=0;
			if(a[per[0]]==a[per[1]]&&a[per[1]]==a[per[2]]||a[per[0]]+1==a[per[1]]&&a[per[1]]+1==a[per[2]]){
				if(b[per[0]]==b[per[1]]&&b[per[1]]==b[per[2]])set++;
			}
			if(a[per[3]]==a[per[4]]&&a[per[4]]==a[per[5]]||a[per[3]]+1==a[per[4]]&&a[per[4]]+1==a[per[5]]){
				if(b[per[3]]==b[per[4]]&&b[per[4]]==b[per[5]])set++;
			}
			if(a[per[6]]==a[per[7]]&&a[per[7]]==a[per[8]]||a[per[6]]+1==a[per[7]]&&a[per[7]]+1==a[per[8]]){
				if(b[per[6]]==b[per[7]]&&b[per[7]]==b[per[8]])set++;
			}
//cout<<set<<endl;
			if(set==3)win=true;
		}while(next_permutation(per,per+9));
		cout<<(win?1:0)<<endl;
	}
	return 0;
}