#include <stdio.h>

int main(void)
{
	int card[3][9]={0};
	int tmp[9],ntmp;
	char mark;
	int n,i,j,k;
	int Rc=0,Gc=0,Bc=0,hantei;
	
	scanf("%d",&n);
	
	for(i=0;i<n;i++){
		for(j=0;j<9;j++){
			scanf("%d",&tmp[j]);
		}
		for(k=0;k<9;k++){
			scanf(" %c",&mark);
			switch(mark){
				case 'R' :
					card[0][Rc]=tmp[k];
					//printf("R%d ",card[0][Rc]);
					Rc++;
					break;
				case 'G' :
					card[1][Gc]=tmp[k];
					//printf("G%d ",card[1][Gc]);
					Gc++;
					break;
				case 'B' :
					card[2][Bc]=tmp[k];
					//printf("B%d ",card[2][Bc]);
					Bc++;
					break;
			}
			
		}
	}
/*????????????*/
for(i=0;i<3;i++){
  for (j=0; j<9; ++j) {
    for (k=j+1; k<9; ++k) {
      if (card[i][j] > card[i][k]&&card[i][k]!=0) {
        ntmp =  card[i][j];
        card[i][j] = card[i][k];
        card[i][k] = ntmp;
      }
    }
  }
}
/*???????????°??¨
for(i=0;i<3;i++){
	for (j=0; j<9; ++j) {
		printf("%d",card[i][j]);
	}
}
*/
printf("\n");
	for(k=0;k<n;k++){
		if(Rc%3==0&&Gc%3==0&&Bc%3==0){
			for(i=0;i<3;i++){
				for (j=0; j<9; j+=3) {
					if(((card[i][j]==card[i][j+1]-1)&&(card[i][j]==card[i][j+2]-2))||(card[i][j]==card[i][j+1])&&(card[i][j]==card[i][j+2])){
						/*????????¶????????£?????????????????°???????????????*/
						if(card[i][j]!=0||card[i][j+1]!=0||card[i][j+2]!=0) hantei++;;
						
					}
					
				}
				
			}
			if(hantei>=3){
				printf("1\n");
			}else if(hantei<3){printf("0\n");}
			hantei=0;
		}else{
			printf("5\n");
		}
	}


	
	return 0;
}

/*
???????????????????????? 9????????? 1???9 4?????????
?????????9??????????????????
???????????????1?????¨?????????1??????????????????
???1*3??????123???1?????????
???1*2???1???0?????????  ?????????????????¨??


?????????¨?????????
????????????§?????????%3??§????????????
?°?????????????????????????
??°????????????




*/