import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * <pre>
 *AOJ Rummy?????????????????????????????????????????????????????°
 * </pre>
 *
 * @author ??????
 * @category AOJRummy
 * @version 1.8_45
 * @since 1.8_45
 */
public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {

		// ?????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// ?????????????´???´???
		ArrayList<Integer> answer = new ArrayList<Integer>();

		// ?????????????????°
		int cardhuda = 9;

		// 1??????????????????set?????°??????????????°
		int set = Integer.parseInt(br.readLine());

		// for????????? set????????°?????????
		for (int cnt1 = 0; cnt1 < set; cnt1++) {
			/**
			 * @param cardArr
			 *            ????´???´???
			 * @param tmpNum
			 *            ??°???????????±
			 * @param tmpColor
			 *            ???????????±
			 */
			ArrayList<UTRummyCard> cardArr = new ArrayList<UTRummyCard>();

			// ??????????????°??????????????¨????????????
			String tmpNum = br.readLine();
			// ??????????????????????????¨????????????
			String tmpColor = br.readLine();

			/**
			 * ?????°???????§£ ??°??????????´?
			 *
			 * @param transNum
			 *            ??°???????????±
			 * @param transColor
			 *            ???????????±
			 */
			String[] transNum = tmpNum.split(" ");
			String[] transColor = tmpColor.split(" ");

			// ?????????????????°?????????
			for (int cnt2 = 0; cnt2 < cardhuda; cnt2++) {
				boolean tool = false;
				// ????´??????????????????????????????????????????????
				// ????¨?????????????????????\
				cardArr.add(new UTRummyCard(Integer.parseInt(transNum[cnt2]), transColor[cnt2], tool));
			}

			/**
			 * ???????????¶??????????????°??? ????????????????????°0
			 *
			 * @param result
			 *            ?????????????????????????´???´???
			 */
			int result = utRummy(cardArr);
			// ?????????????´?
			answer.add(result);
		}
		// ???????????????
		outputAnswer(answer);
	}// ?????????????????????????????????

	/**
	 * ????????????
	 *
	 * @param answer
	 *            ?????????????´???´???
	 */
	private static void outputAnswer(ArrayList<Integer> answer) {

		// for????????? ??????????????????????????????
		for (int cnt = 0; cnt < answer.size(); cnt++) {
			// ???????????????
			System.out.println(answer.get(cnt));
		}
	}// outputAnswer????????????????????????

	/**
	 * ????????????????????´??? ??????????¨?
	 *
	 * @return result ??????????????????????????????
	 */
	private static int utRummy(ArrayList<UTRummyCard> cardArr) {

		// ?????????0???????????????
		int result = 0;

		// ????????????????????°???????????????
		cardNumSort(cardArr);
		// ?????????????????????????????????
		cardColorSort(cardArr);

		// ?????£?¶?
		serialCard(cardArr);

		// ????????°?????????
		sameCard(cardArr);

		int toolcnt = 0;
		for (int cnt = 0; cnt < cardArr.size(); cnt++) {
			if (cardArr.get(cnt).isUse() == false)
				toolcnt++;
		}
		return result = toolcnt == 0 ? 1 : 0;

	}// utRummy????????????????????????

	private static void sameCard(ArrayList<UTRummyCard> cardArr) {

		for (int cnt1 = 0; cnt1 < cardArr.size() - 2; cnt1++) {
			for (int cnt2 = cnt1 + 1; cnt2 < cardArr.size() - 1; cnt2++) {
				for (int cnt3 = cnt2 + 1; cnt3 < cardArr.size(); cnt3++) {
					// (1)?????????????????????????????????
					if (cardArr.get(cnt1).getFrontnumber() == cardArr.get(cnt2).getFrontnumber()
							&& cardArr.get(cnt2).getFrontnumber() == cardArr.get(cnt3).getFrontnumber()) {
						// (2)????????°????????????????????????
						if (cardArr.get(cnt1).getRearColor().equals(cardArr.get(cnt2).getRearColor())
								&& cardArr.get(cnt2).getRearColor().equals(cardArr.get(cnt3).getRearColor())) {
							// (3)????¨????????????????
							if (cardArr.get(cnt1).isUse() == false && cardArr.get(cnt2).isUse() == false
									&& cardArr.get(cnt3).isUse() == false) {

								for (int tool = cnt1; tool <= cnt3; tool++) {
									cardArr.get(tool).setUse(true);
								}
							}
						}
					}
				}
			}
		}
	}

	private static void serialCard(ArrayList<UTRummyCard> cardArr) {

		for (int cnt1 = 0; cnt1 < cardArr.size() - 2; cnt1++) {
			for (int cnt2 = cnt1 + 1; cnt2 < cardArr.size() - 1; cnt2++) {
				for (int cnt3 = cnt2 + 1; cnt3 < cardArr.size(); cnt3++) {
					// (1)?????????????????????????????????
					if (cardArr.get(cnt1).getRearColor().equals(cardArr.get(cnt2).getRearColor())
							&& cardArr.get(cnt2).getRearColor().equals(cardArr.get(cnt3).getRearColor())) {
						// (2)3??£?¶??????°????????????
						if (cardArr.get(cnt1).getFrontnumber() == cardArr.get(cnt2).getFrontnumber() - 1
								&& cardArr.get(cnt1).getFrontnumber() == cardArr.get(cnt3).getFrontnumber() - 2) {
							// (3)????¨????????????????
							if (cardArr.get(cnt1).isUse() == false && cardArr.get(cnt2).isUse() == false
									&& cardArr.get(cnt3).isUse() == false) {

								cardArr.get(cnt1).setUse(true);
								cardArr.get(cnt2).setUse(true);
								cardArr.get(cnt3).setUse(true);
							}
						}
					}
				}
			}
		}
	}

	/**
	 * ??????????????´???
	 *
	 * @param cardArr
	 *            ??´???????±????????????????
	 */
	private static void cardColorSort(ArrayList<UTRummyCard> cardArr) {

		for (int cnt1 = 0; cnt1 < cardArr.size() - 1; cnt1++) {
			for (int cnt2 = 0; cnt2 < cardArr.size() - 1 - cnt1; cnt2++) {
				if (cardArr.get(cnt2).getRearColor().charAt(0) < cardArr.get(cnt2 + 1).getRearColor().charAt(0)) {
					// ?????????????????????????????°
					UTRummyCard tmp = cardArr.get(cnt2);
					cardArr.set(cnt2, cardArr.get(cnt2 + 1));
					cardArr.set(cnt2 + 1, tmp);
				}
			}
		}
	}// cardColorSort????????????????????????

	/**
	 * ??????????????´???
	 *
	 * @param cardArr
	 *            ??´???????±????????????????
	 */
	private static void cardNumSort(ArrayList<UTRummyCard> cardArr) {
		for (int cnt1 = 0; cnt1 < cardArr.size() - 1; cnt1++) {
			for (int cnt2 = 0; cnt2 < cardArr.size() - 1 - cnt1; cnt2++) {
				if (cardArr.get(cnt2).getFrontnumber() > cardArr.get(cnt2 + 1).getFrontnumber()) {
					// ?????????????????????????????°
					UTRummyCard tmp = cardArr.get(cnt2);
					cardArr.set(cnt2, cardArr.get(cnt2 + 1));
					cardArr.set(cnt2 + 1, tmp);
				}
			}
		}
	}// cardSort????????????????????????

}// Rummy4?????????????????????

/**
 * ?????????Card
 *
 * @author ?????????????????§???
 */
class UTRummyCard {

	/**
	 * int ??????????????°????????\????????±????????\???
	 */
	private int frontnumber = 0;

	/**
	 * String ????????????????????\????????±????????\???
	 */
	private String rearColor = "";

	/**
	 * ????????????????¨??????????
	 */
	private boolean isUse = false;

	public int getFrontnumber() {
		return frontnumber;
	}

	public void setFrontnumber(int frontnumber) {
		this.frontnumber = frontnumber;
	}

	public String getRearColor() {
		return rearColor;
	}

	public void setRearColor(String rearColor) {
		this.rearColor = rearColor;
	}

	public UTRummyCard() {

	}

	public UTRummyCard(String white) {
		this.rearColor = white;
	}

	public boolean isUse() {
		return isUse;
	}

	public void setUse(boolean isUse) {
		this.isUse = isUse;
	}

	/**
	 * ????????????????????????????????????????????????????????°??¨??????int,String???
	 *
	 * @param num
	 *            ???????????????????????????????????°???
	 * @param color
	 *            ????????????????????????????????????
	 */
	public UTRummyCard(int num, String color, boolean tool) {
		frontnumber = num;
		rearColor = color;
		isUse = tool;
	}
}// UTRummyCard?????????????????????

Measured memory_bytes: 25320