#include <iostream>
#include <algorithm>
#define N 9
using namespace std;
void NP(int);
void check();
int flag;
pair<char,char> data[N],out[N];
bool used[N];
int main(){
  int T;
  cin >> T;
  for(int i=0;i<T;i++){
    for(int j=0;j<N;j++){
      cin >> data[j].first;
    }
    for(int j=0;j<N;j++){
      cin >> data[j].second;
    }
    flag=0;
    NP(0);
    if(flag==1) cout << 1 << endl;
    else cout << 0 << endl;
  }
  return 0;
}
void NP(int x){
  if(flag==1) return ;
  if(x<N){
    for(int i=0;i<N;i++){
      if(used[i]) continue;
      out[x]=data[i];
      used[i]=true;
      NP(x+1);
      used[i]=false;
    }
  }else{
    check();
  }
}
void check(){
  int flag1=0;
  for(int i=0;i<N;i+=3){
    if(out[i].second==out[i+1].second&&out[i+1].second==out[i+2].second){
      if(out[i].first==out[i+1].first-1&&out[i+1].first==out[i+2].first-1){
      }
      else if(out[i].first==out[i+1].first&&out[i+1].first==out[i+2].first){
      }
      else{
	flag1=1;
	break;
      }
    }else{
      flag1=1;
      break;
    }
  }
  if(flag1==0) flag=1;
}