Your friend recently came up with a card game called UT-Rummy.

The cards used in this game are numbered red, green, or blue and any number from 1 to 9. Each player in this game has 9 cards in his or her hand, chooses one from his or her hand, discards it, and draws one from the deck instead. In this way, the turn is advanced in order, and the player who first makes three "sets" of three cards on hand wins. A set is a set of three cards of the same color, all having the same number or serial numbers. Patrol of numbers is not allowed for serial numbers. For example, 7, 8 and 9 are serial numbers, but 9, 1 and 2 are not.

Your friend has planned to sell this game as a computer game, and as part of that, he has asked you to make a judgment part of the victory conditions. Your job is to write a program that determines if your hand meets the winning conditions.



Input

The first line of input is given the number T (0 <T ≤ 50), which represents the number of datasets. This line is followed by T datasets.

Each dataset consists of two lines, the first line is given the number ni of the i-th (i = 1, 2, ..., 9) card, each separated by a space, and the second line is i. The letters ci representing the color of the second card are given, separated by spaces. The number ni on the card is an integer that satisfies 1 ≤ ni ≤ 9, and the color ci on the card is one of the letters "` R` "," `G`", and "` B` ", respectively.

No more than 5 cards with the same color and number will appear in one dataset.

Output

For each dataset, output "` 1` "if the winning conditions are met, otherwise output" `0`".

Example

Input

5
1 2 3 3 4 5 7 7 7
R R R R R R G G G
1 2 2 3 4 4 4 4 5
R R R R R R R R R
1 2 3 4 4 4 5 5 5
R R B R R R R R R
1 1 1 3 4 5 6 6 6
R R B G G G R R R
2 2 2 3 3 3 1 1 1
R G B R G B R G B


Output

1
0
0
0
1
import java.util.Scanner;

public class Main {

	static void init_array ( int[][] array ) {

		for ( int i = 0 ; i < array.length ; i++ ) {
			for( int j = 0 ; j < array[i].length ; j++ ) {
				array[i][j] = 0;
			}
		}
	}

	static void set_card_num ( int[] card_num, String[] card_color, int[][] card ) {

		int color = 0;

		for ( int i = 0 ; i < card_num.length ; i++ ) {
			switch( card_color[i] ) {
			case"R":
				color = 0;
				break;
			case"G":
				color = 1;
				break;
			case"B":
				color = 2;
				break;
			}

			card[color][card_num[i]]++;
			card[color][0]++;
		}
	}

	static boolean judge ( int[][] card ) {

		boolean judge = true;

		for ( int i = 0 ; i < card.length ; i++ ) {
			if( ( card[i][0] ) != 0 && judge ) {
				if ( card[i][0] % 3 != 0 ) {
					judge = false;
				}
				if( judge ) {
					for( int j = 1 ; j <  card[i].length ; j++ ) {
						if ( judge ) {
							if( card[i][j] < 0 ) {
								judge = false;
							} else	if( card[i][j] > 0 ) {
								if( card[i][j] >= 3 ) {
									card[i][j] -= 3;
								}

								if ( card[i][j] != 0 ) {
									if( j == ( card[i].length - 1 ) || j == ( card[i].length - 2 ) ) {
										if ( ( card[i][j] % 3 ) != 0 ) {
											judge = false;
										}
									} else {
										card[i][j+2] -= card[i][j];
										card[i][j+1] -= card[i][j];
									}
								}
							}
						}
					}
				}
			}
		}
		return judge;
	}

	static void judge_output ( boolean judge ) {
		if ( judge ) {
			System.out.println( "1" );
		} else {
		System.out.println( "0" );
		}
	}

	public static void main(String[] args) {

		//勝敗判定 true 勝利 false 敗北
		boolean win_match;
		//データセット数
		int set_num = 0;
		//手持ちのカード数
		int num_of_onhand = 9;
		//カードの数字の最大値
		int card_num_max = 9;
		//色の種類の数
		//色に対応した数字 R=0,G=1,B=2
		int color_num = 3;

		//1つのデータセットのカードの数字 配列nは手持ちn枚目
		int[] input_card_num = new int [num_of_onhand];
		//1つのデータセットのカードの色 配列nは手持ちn枚目
		String[] input_card_color = new String [num_of_onhand];

		//ある色の数字のカード枚数 R1の枚数が1枚 card[0][1]=1;
		//ただし数字0は1つのデータセットの同色の合計数 Rの合計数3枚 card[0][0]=3;
		int[][] card = new int[color_num][card_num_max+1];

		Scanner sc= new Scanner( System.in );

		//データセット数読み込み
		set_num = sc.nextInt();

		//データセットの回数、処理実行
		for ( int i = 0 ; i < set_num ; i++ ) {

			//初期化
			win_match = true;
			init_array ( card );

			//データセット（数字）読み込み
			for ( int j = 0 ; j < num_of_onhand ; j++ ) {
				input_card_num[j] = sc.nextInt();
			}

			//データセット（色）読み込み
			for ( int j = 0 ; j < num_of_onhand ; j++ ) {
				input_card_color[j] = sc.next();
			}

			//データセットを元に色と数字に対応したカードの枚数をセット
			//同色の合計枚数をセット
			set_card_num( input_card_num, input_card_color, card );

			//勝敗判定 true 勝利 false 敗北
			//敗北条件を判定し、一つでも一致したら敗北とみなす
			win_match = judge( card );

			//結果出力
			judge_output ( win_match );

		}
		sc.close();
	}
}

