#include <stdio.h>

struct Card{
  int n;
  char c;
};

int main(void){
  int i,j,n,set;
  struct Card x[9],tmp;

  scanf("%d ",&n);
  while(n--){
    for(i=0;i<9;i++){
      scanf("%d ",&x[i].n);
    }
    for(i=0;i<9;i++){
      scanf("%c ",&x[i].c);
    }

    for(i=0;i<8;i++){
      for(j=i+1;j<9;j++){
	if(x[i].c > x[j].c){
	  tmp = x[i];
	  x[i] = x[j];
	  x[j] = tmp;
	}
	else if(x[i].c == x[j].c && x[i].n > x[j].n){
	  tmp = x[i];
	  x[i] = x[j];
	  x[j] = tmp;
	}
      }
    }

    set = 0;
    for(i=0;i<9;i+=3){
      if(x[i].c==x[i+1].c && x[i].c==x[i+2].c
      &&(x[i].n==x[i+1].n && x[i].n==x[i+2].n
      || x[i].n+1==x[i+1].n && x[i].n+2==x[i+2].n)){
	set++;
      }
    }
    if(set == 3){
      printf("1\n");
    }
    else{
      printf("0\n");
    }
  }

  return 0;
}