#include #include using namespace std; class Peace { public: char N,W,E,S; Peace() { } Peace(string str) { N = str[0]; E = str[1]; S = str[2]; W = str[3]; } void rotate() { char tmp = W; W = S; S = E; E = N; N = tmp; } }; Peace peace[9]; int Cnt; int checked[9]; Peace table[9]; bool check(int dep) { switch(dep) { case 0: return true; case 1: case 2: return (table[dep].W != table[dep-1].E && toupper(table[dep].W) == toupper(table[dep-1].E)); case 3: case 6: return (table[dep].N != table[dep-3].S && toupper(table[dep].N) == toupper(table[dep-3].S)); case 4: case 5: case 7: case 8: return ((table[dep].W != table[dep-1].E && toupper(table[dep].W) == toupper(table[dep-1].E)) && (table[dep].N != table[dep-3].S && toupper(table[dep].N) == toupper(table[dep-3].S))); } return false; } void saiki(int dep) { if(dep == 9) { Cnt++; } else { // dep = 0 ~ 8 for(int k = 0; k < 9; k++) { if(checked[k]) { continue; } for(int i = 0; i < 4; i++) { peace[k].rotate(); table[dep] = peace[k]; if(check(dep)) { checked[k] = 1; saiki(dep+1); checked[k] = 0; } } } } } int main() { int loop; cin >> loop; while(loop--) { for(int i = 0; i < 9; i++) { string str; cin >> str; peace[i] = Peace(str); } Cnt = 0; memset(checked, 0, sizeof(checked)); saiki(0); cout << Cnt << endl; } }