#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };

int dfs(vector<vector<vector<int>>>&field, int nowx, int nowy,bitset<9>&used,const vector<vector<int>>&pieces) {
	if (nowy == 3)return 1;
	else {
		int ans = 0;
		for (int i = 0; i < 9; ++i) {
			if (!used[i]) {
				used[i] = true;
				vector<int>v(pieces[i]);
				for (int way = 0; way < 4; ++way) {
					rotate(v.begin(), v.begin() + 1, v.end());
					bool ok = true;
					if (nowx) {
						if (field[nowy][nowx - 1][2] + v[0]) {
							ok = false;
						}
					}
					if (nowy) {
						if (field[nowy - 1][nowx][1] + v[3]) {
							ok = false;
						}
					}
					if (ok) {
						field[nowy][nowx] = v;
						ans += dfs(field, (nowx + 1) % 3, nowx == 2 ? nowy + 1 : nowy, used, pieces);
						field[nowy][nowx].clear();
					}
				}
				used[i] = false;
				
			}
		}
		return ans;
	}
}
int main() {
	int N; cin >> N;
	map<int, int>mp;
	mp['W'] = 1;
	mp['R'] = 2;
	mp['G'] = 3;
	mp['B'] = 4;
	mp['w'] = -1;
	mp['r'] = -2;
	mp['g'] = -3;
	mp['b'] = -4;
	while (N--) {
		vector<vector<int>>pieces(9);
		for (int i = 0; i < 9; ++i) {

			string st; cin >> st;
			for (int j = 0; j < 4; ++j) {
				pieces[i].push_back(mp[st[j]]);
			}
		}
		bitset<9>used;
		vector<vector<vector<int>>>field(3, vector<vector<int>>(3));
		int ans = dfs(field, 0, 0, used, pieces);
		cout << ans << endl;
	}
	return 0;
}