#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s); sin>>v; return v;}
template<class T> inline string toString(T x) {ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);


bool paired(vector<pii> &cards,int from){
	if(cards[from].second==cards[from+1].second&&cards[from+2].second==cards[from+1].second){
		if(cards[from+2].first+2==cards[from+1].first+1&&cards[from+1].first+1==cards[from].first){
			return true;
		}else if(cards[from].first==cards[from+1].first&&cards[from+2].first==cards[from+1].first){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}
int main(){
	int t;
	cin>>t;
	REP(test,t){
		vector<pii> cards(9);
		REP(i,9){
			cin>>cards[i].first;
		}
		REP(i,9){
			char c;
			cin>>c;
			cards[i].second=c;
		}
		sort(ALL(cards));
		bool flag=false;
		do{
			if(paired(cards,0)&&paired(cards,3)&&paired(cards,6)){
				flag=true;
				break;
			}
		}while(next_permutation(ALL(cards)));
		cout<<(flag?1:0)<<endl;
	}
}