import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		//?????£?????????
		int output;
		int loop;
		String line;
		Card[] cardList = new Card[9];

		Scanner sc = null;

		try {
			sc = new Scanner(System.in);

			//?????§???????????°???????????????
			loop = sc.nextInt();
			//?¬???????????§????
			line = sc.nextLine();
			for (int i = 0; i < loop; i++) {
				//?????????????????????
				line = sc.nextLine();
				String[] work = line.split(" ");

				int[] number = new int[work.length];
				for (int j = 0; j < work.length; j++) {
					number[j] = Integer.parseInt(work[j]);
				}

				line = sc.nextLine();
				String[] color = line.split(" ");

				//???????????????
				for (int j = 0; j < cardList.length; j++) {
					cardList[j] = new Card(number[j], color[j]);
				}
				//?????????????????°?????§?????????
				Arrays.sort(cardList , new Comparator<Card>() {

					@Override
					public int compare(Card card1, Card card2) {

						int temp = card1.getColor().compareTo(card2.getColor());

						if (temp == 0) {
							return card1.getNumber() - card2.getNumber();
						}
						return temp;
					}
				});

				//???????????¶????????????????????????????????§??????
				output = 0;
				Check che = new Check();
				output = che.cardCheck(cardList);
				//???????????????
				System.out.println(output);
			}
		} finally {
			sc.close();
		}

	}

	//??????????????????
	static class Card {
		//?????£?????????
		//??????????????°???
		private int number;
		//???????????????
		private String color;

		//?????????????????????
		public  Card(int num, String color){
			this.number = num;
			this.color = color;
		}

		//????????????????????????
		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

	}

	//?????????????????¨??????????????????
	static class Check {

		public int cardCheck(Card[] ca){
			int count = 0;
			int anser = 0;
			Card[] list = ca;
			int[] checklist = {0,0,0,0,0,0,0,0,0};

			int loop = list.length;

			for (int i = 0; i < loop - 2; i++) {
				for (int j = i + 1; j < loop - 1; j++) {
					for (int k = j + 1; k < loop; k++) {

						if (checklist[i] == 0 && checklist[j] == 0 && checklist[i] == 0) {
							//???????¢????
							if (list[i].getColor().equals(list[j].getColor())
									&& list[j].getColor().equals(list[k].getColor())) {
								//??£???????????????
								if (list[i].getNumber() + 1 == list[j].getNumber()) {
									if (list[j].getNumber() + 1 == list[k].getNumber()) {
										checklist[i] = 1;
										checklist[j] = 1;
										checklist[k] = 1;

										count++;
									}
								}
								//????????°???????????????
								if (list[i].getNumber() == list[j].getNumber()) {
									if (list[j].getNumber() == list[k].getNumber()) {
										checklist[i] = 1;
										checklist[j] = 1;
										checklist[k] = 1;
										count++;
									}
								}
							}
						}
					}

				}
			}
			if (count == 3) {
				anser = 1;
			}
			return anser;
		}
	}
}

Measured memory_bytes: 26792