#include <iostream>
#include <algorithm>

using namespace std;

struct Card {
	int  n;
	char c;
};

int judge( Card a, Card b, Card c )
{
	if(a.n == b.n && b.n == c.n) return true;
	int sum = a.n + b.n + c.n;
	int Min = min(a.n , min(b.n, c.n));
	if(sum == (3*Min+3)) return true;
	return false;
}

const int SIZE = 9;
Card cards[SIZE];
bool locked[SIZE], answer;
void search( int count )
{
	if(count >= 4) return;
	for(int i=0; i < SIZE && !answer; ++i) {
		if(locked[i]) continue;
		locked[i] = true;
		for(int j=0; j < SIZE && !answer; ++j) {
			if(locked[j] || cards[i].c != cards[j].c) continue;
			locked[j] = true;
			for(int k=0; k < SIZE && !answer; ++k) {
				if(locked[k] || cards[j].c != cards[k].c) continue;
				locked[k] = true;
				if(judge(cards[i], cards[j], cards[k])) {
					if(count == 3) { answer = true; return; }
					else { search(count + 1); }
				}
				locked[k] = false;
			}
			locked[j] = false;;
		}
		locked[i] = false;
	}
}

int main( void )
{
	int t;
	cin >> t;
	while(t--) {
		for(int i=0; i < SIZE; ++i) cin >> cards[i].n;
		for(int i=0; i < SIZE; ++i) { cin >> cards[i].c; locked[i] = false; }
		answer = false;
		search(1);
		if(answer)
			cout << 1 << endl;
		else
			cout << 0 << endl;
	}
	return 0;
}