#include #include #include #include #include using namespace std; int PIECES[9][4]; vector > INDEX; int ANSWER; int USED[9]; set > > CACHE; bool Adapt(int a, int b) { bool res = (abs(a - b) == abs('a' - 'A')); //cerr << (char)a << (char)b << res << " "; return res; } void Rec(int pos) { //cerr << pos << " "; if (pos == 9){ if (CACHE.find(INDEX) == CACHE.end()){ ++ANSWER; CACHE.insert(INDEX); } return; } int x = pos % 3; int y = pos / 3; for (int i = 0; i < 9; ++i){ if (USED[i]){ continue; } for (int rot = 0; rot < 4; ++rot){ if (0 < y){ int up_index = INDEX[pos - 3].first; int up_rot = INDEX[pos - 3].second; int up_num = PIECES[up_index][(2 + up_rot) % 4]; int now_num = PIECES[i][rot]; if (!Adapt(up_num, now_num)){ continue; } } if (0 < x){ int left_index = INDEX.back().first; int left_rot = INDEX.back().second; int left_num = PIECES[left_index][(1 + left_rot) % 4]; int now_num = PIECES[i][(rot + 3) % 4]; if (!Adapt(left_num, now_num)){ continue; } } INDEX.push_back(make_pair(i, rot)); USED[i] = true; Rec(pos + 1); USED[i] = false; INDEX.pop_back(); } } } main() { int loop; cin >> loop; while (loop--){ for (int i = 0; i < 9; ++i){ for (int j = 0; j < 4; ++j){ char c; cin >> c; PIECES[i][j] = (int)c; } } memset(USED, 0, sizeof(USED)); CACHE.clear(); ANSWER = 0; Rec(0); cout << ANSWER << endl; } }