#include <iostream>
#include <string>
#include <cmath>
using namespace std;
char mp[3][3][4];
string str[10];
bool used[9];long long cnt,ans;
void saiki2(int num){
  int ex[] ={0,0,2,2};
  int ey[] ={2,0,0,2};
  int tx[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
  int ty[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};
  if(num == 4) ans++;
  else
    for(int i=0;i<9;i++){
      if(used[i] == 1) continue;
      for(int j=0;j<4;j++){
	int nx = ex[num]+tx[num][0];
	int nx2 = ex[num]+tx[num][1];
	int ny = ey[num]+ty[num][0];
	int ny2 = ey[num]+ty[num][1];
	if(abs(str[i][j] - mp[ny][nx][(num+3)%4])=='a'-'A' &&
	   abs(str[i][(j+3)%4] - mp[ny2][nx2][(num+2)%4]) == 'a'-'A'){
	  
	  used[i] = 1;
	  saiki2(num+1);
	  used[i] = 0;
	}	 
      }
    }
}

void saiki(int num){
int dx[] = {0,-1,0 ,1};
int dy[] = {1,0,-1,0};
  if(num == 4) {
    saiki2(0);
    return;
  }
  for(int i=0;i<9;i++){
    if(used[i] == 1) continue;
    for(int j=0;j<4;j++)
      if(abs(mp[1][1][(num+2)%4] - str[i][j])=='a'-'A') {
	for(int k=0;k<4;k++) mp[1+dy[num]][1+dx[num]][(k+num)%4] = str[i][(j+k)%4];
	used[i] = 1;
	saiki(num+1);
	used[i] = 0;
      }
  }
}

int main(){
  int n;
  cin >> n;
  while(n--){
    ans = 0;
  for(int i=0;i<9;i++) cin >> str[i];
  for(int i=0;i<=9;i++){
    for(int j=0;j<4;j++) mp[1][1][j] = str[i][j];
    used[i] = 1;
    saiki(0);
    used[i] = 0;
  }
  cout << ans*4<<endl;
  }
  return 0;     
}