#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
using namespace std;

// value, color
using Card = pair<int, char>;

const int S = 9;
bool ansFlag = true;

bool check(vector< Card > cards) {
  char col = cards[0].second;

  // check (color)
  for(int k=0; k<3; k++) {
    if(cards[k].second != col) {
      return false;
    }
  }

  // check (number)
  bool ok1 = true, ok2 = true;
  sort(cards.begin(), cards.end());
  int val = cards[0].first;

  // same number
  for(int k=0; k<3; k++) {
    if(cards[k].first != val) {
      ok1 = false;
    }
  }

  // stair
  for(int k=1; k<3; k++) {
    // printf("val = %d, cur = %d\n", val, cards[k].first);
    if(cards[k].first != val + 1) {
      ok2 = false;
    }
    val = cards[k].first;
  }

  return ok1 || ok2;
}

void solve(vector< Card > cards, int iter, vector<int> checked) {
  /*
  printf("iter = %d:", iter);
  for(int i=0; i<S; i++) {
    printf(" %d", checked[i]);
  }
  printf("\n");
  */

  if(iter == 3) {
    ansFlag = true;
    return;
  }
  for(int i=0; i<9; i++) {
    if(checked[i]) continue;
    for(int j=i+1; j<9; j++) {
      if(checked[j]) continue;
      for(int k=j+1; k<9; k++) {
        if(checked[k]) continue;
        assert(i != j && j != k && k != i);
        if(check(vector< Card >{cards[i], cards[j], cards[k]})) {
          checked[i] = checked[j] = checked[k] = true;
          solve(cards, iter+1, checked);
          checked[i] = checked[j] = checked[k] = false;
        }
      }
    }
  }
}

int main() {
  int T; cin >> T;
  while(T--) {
    vector<int> vals(S);
    vector<char> chrs(S);

    vector< Card > cards(S);
    for(int i=0; i<S; i++) cin >> vals[i];
    for(int i=0; i<S; i++) cin >> chrs[i];
    for(int i=0; i<S; i++) cards[i] = make_pair(vals[i], chrs[i]);

    vector<int> checked(S);
    ansFlag = false;
    solve(cards, 0, checked);

    cout << (ansFlag ? 1 : 0) << endl;
  }
  return 0;
}

