import std.stdio;
import std.ascii;
import core.bitop;
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.random;
import std.regex;
import std.typecons;

const dy = [-1, 0, 1, 0],
      dx = [0, 1, 0, -1];

string[] P;
void input() {
    P = readln.chomp.split(" ").array;
}
void solve() {
    int Ans = 0;
    string[] X = new string[9];
    bool check(int y, int x) {
        for (int i = 0, k = 3; i < 2; i++, k = (k + 1) % 4) {
            int ny = y + dy[k],
                nx = x + dx[k];
            if (ny < 0 || ny >= 3) continue;
            if (nx < 0 || nx >= 3) continue;
            //writeln([y, x], " ", [ny, nx]);
            char a = X[y * 3 + x][k],
                 b = X[ny * 3 + nx][(k + 2) % 4];
            if (a == b || std.ascii.toUpper(a) != std.ascii.toUpper(b)) return false;
        }
        return true;
    }

    string rotate(in string s) {
        return s[3] ~ s[0 .. 3];
    }

    void dfs(int v, int used) {
        //writefln("%d: %09b", v, used);
        if (v == 9) {
            Ans++;
            return;
        }

        int y = v / 3,
            x = v % 3;

        foreach (i; 0 .. 9) {
            if (used & (1 << i)) continue;
            X[v] = P[i];
            //writeln(X[v], " ", rotate(X[v]));
            for (int k = 0; k < 4; k++, X[v] = rotate(X[v])) {
                if (check(y, x)) {
                    dfs(v + 1, used | (1 << i));
                }
            }
        }

    }
    dfs(0, 0);
    writeln(Ans);
}

void main() {
    int N;
    scanf("%d\n", &N);
    foreach (i; 0 .. N) {
        input; solve;
    }
}