#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)),(x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

const int MAX = 200000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const int MOD = 1000000007;

int ns[9];

void solve(void){
    int T;
    cin >> T;
    rep(T, i) {
        rep(9, i) scanf("%d", &ns[i]);
        char c;
        rep(9, i) {
            cin >> c;
            ns[i] += (c-'A')*10;
        }
        sort(ns, ns+9);
        bool can = false;
        do {
            bool t = true;
            rep(3, i) {
                t = t && ((ns[3*i] == ns[3*i+1] && ns[3*i] == ns[3*i+2]) || (ns[3*i] == ns[3*i+1]+1 && ns[3*i] == ns[3*i+2]+2));
            }
            can = can || t;
        } while(next_permutation(ns, ns+9));
        cout << (can?"1":"0") << endl;
    }
}

int main(void){
  solve();
  return 0;
}

