import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

int[] X;
char[] Y;
void input() {
    X = readln.chomp.split(" ").map!(to!int).array;
    Y = cast(char[])(readln.chomp.split(" ").map!"a[0]".array);
}

void solve() {
    auto used = new bool[9];
    bool dfs() {
        foreach (i; 0 .. 9) {
            foreach (j; 0 .. 9) {
                foreach (k; 0 .. 9) {
                    if (i == j || j == k || k == i) continue;
                    if (used[i] || used[j] || used[k]) continue;
                    if (X[i] + 1 == X[j] && X[j] + 1 == X[k] &&
                            Y[i] == Y[j] && Y[j] == Y[k]) {
                        used[i] = used[j] = used[k] = true;
                        if (dfs) return true;
                        used[i] = used[j] = used[k] = false;
                    }
                }
            }
        }
        foreach (i; 0 .. 9) {
            foreach (j; 0 .. 9) {
                foreach (k; 0 .. 9) {
                    if (i == j || j == k || k == i) continue;
                    if (used[i] || used[j] || used[k]) continue;
                    if (X[i] == X[j] && X[j] == X[k] &&
                            Y[i] == Y[j] && Y[j] == Y[k]) {
                        used[i] = used[j] = used[k] = true;
                        if (dfs) return true;
                        used[i] = used[j] = used[k] = false;
                    }
                }
            }
        }
        return used.count(true) == 9;
    }
    writeln(cast(int)(dfs));
}
 
void main() {
    int T; scanf("%d\n", &T);
    foreach (t; 0 .. T) {
        input; solve;
    }
}