#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
int ans,sp[3][3];
char pc[9][5];
bool use[9];

void rec(int c)
{
	if(c==9)
	{
		ans++; return;
	}
	rep(i,9)if(!use[i])
	{
		sp[c/3][c%3]=i; use[i]=1;
		rep(rt,4)
		{
			rep(d,4)
			{
				int y=c/3+dy[d],x=c%3+dx[d];
				if(y<0||x<0||y>=3||x>=3||sp[y][x]<0)continue;
				char c1=pc[i][d],c2=pc[sp[y][x]][d^2];
				if(abs(c1-c2)!=abs('A'-'a'))goto FAIL;
			}
			rec(c+1);
			FAIL:
			rotate(pc[i],pc[i]+1,pc[i]+4);
		}
		sp[c/3][c%3]=-1; use[i]=0;
	}
}

int sol()
{
	rep(i,9)use[i]=0,sp[i/3][i%3]=-1; ans=0;
	rec(0);
	return ans;
}

int main()
{
	int n; scanf("%d",&n);
	rep(i,n)
	{
		rep(j,9)scanf("%s",pc[j]);
		printf("%d\n",sol());
	}
	return 0;
}