#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Card {
  int n;
  char c;
  Card(int n, int c): n(n), c(c) {}
};

int T;
 
int main(){

  cin >> T;
  vector<Card> cs(9, Card(0, '0'));
  while (T-- > 0) {
    for (int i = 0; i < 9; i++) {
      cin >> cs[i].n;
    }

    for (int i = 0; i < 9; i++) {
      cin >> cs[i].c;
    }

    int res = 0;
    int order[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
    do {
      bool flg = true;
      for (int i = 0; i < 9; i += 3) {
        if (cs[order[i]].c == cs[order[i + 1]].c && cs[order[i]].c == cs[order[i + 2]].c) {
          int n1 = cs[order[i]].n, n2 = cs[order[i + 1]].n, n3 = cs[order[i + 2]].n;
          if ( (n1 == n2 && n2 == n3) || ( (n2 - n1) == 1 && (n3 - n2) == 1) ) {

          } else {
            flg = false;
            break;
          }
        } else {
          flg = false;
          break;
        }
      }

      if (flg) {
        res = 1;
        break;
      }

    } while (next_permutation(order, order + 9));

    cout << res << endl;

  }

 
  return 0;
}