#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

int main(){
    int n;
    cin >> n;
    rep(p, n){
        vector<pair<string, int>> v;
        vector<int> a(9);
        vector<string> b(9);
        rep(i, 9) cin >> a[i];
        rep(i, 9) cin >> b[i];
        rep(i, 9) v.push_back(make_pair(b[i], a[i]));
        sort(all(v));

        bool possible = false;
        do{
            vector<pair<string, int>> vv = v;
            while(true){
                bool is = false;
                if(v[0] == v[1] && v[1] == v[2]){vector<pair<string, int>> t; rep(i, v.size()-3) t.push_back(v[i+3]); v=t; is = true;}
                if(v.size() == 0) break;
                if(v[0].first == v[1].first && v[1].first == v[2].first && v[0].second == v[1].second-1 && v[1].second == v[2].second-1){vector<pair<string, int>> t; rep(i, v.size()-3) t.push_back(v[i+3]); v=t; is = true;}
                if(!is || v.size() == 0) break;
            }
            if(v.size() == 0){possible = true; break;}
            v = vv;
        }while(next_permutation(all(v)));
        cout << (possible?"1":"0") << endl;
    }
}