#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


class Piece {
public:
  string s;

  string getTurnR(int n) {
    string tmp = s;
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < 4; j++) {
        tmp[j] = s[(j+3)%4];
      }
    }
    return tmp;
  }

  //n=0123:trdl
  char getS(int n) {
    return s[n];
  }
};

Piece p[9];
Piece now[3][3];
int pflag[9];
int num;

void rec1(int);
void rec2(int);

void rec1(int cnt) {
  static int d[4][2] = {{1,0},{2,1},{1,2},{0,1}};
  if(cnt == 4) {
    rec2(0);
    return;
  }
  for(int i = 0; i < 9; i++) {
    if(pflag[i] != 0) continue;
    pflag[i] = 1;
    Piece tp = p[i];
    for(int j = 0; j < 4; j++) {
      if(abs(now[1][1].getS(cnt)-tp.getS((cnt+2)%4)) == 32) {
	now[d[cnt][1]][d[cnt][0]] = tp;
	rec1(cnt+1);
      }
      tp.s = tp.getTurnR(1);
    }
    pflag[i] = 0;
  }
}

void rec2(int cnt) {
  static int d1[4][2] = {{1,0},{2,1},{1,2},{0,1}};
  static int d2[4][2] = {{2,0},{2,2},{0,2},{0,0}};
  if(cnt == 4) {
    num += 4;
    return;
  }

  for(int i = 0; i < 9; i++) {
    if(pflag[i] != 0) continue;
    pflag[i] = 1;
    Piece tp = p[i];
    for(int j = 0; j < 4; j++) {
      if(abs(now[d1[cnt][1]][d1[cnt][0]].getS((cnt+1)%4)
	     -tp.getS((cnt+3)%4)) == 32 &&
	 abs(now[d1[(cnt+1)%4][1]][d1[(cnt+1)%4][0]].getS(cnt)
	     -tp.getS((cnt+2)%4)) == 32) {
	now[d2[cnt][1]][d2[cnt][0]] = tp;
	rec2(cnt+1);
      }
      tp.s = tp.getTurnR(1);
    }
    pflag[i] = 0;
  }
}

int main() {
  int n;
  cin >> n;
  for(int t = 0; t < n; t++) {
    for(int i = 0; i < 9; i++) {
      cin >> p[i].s;
    }
    num = 0;
    for(int i = 0; i < 9; i++) {
      now[1][1] = p[i];
      fill(&pflag[0], &pflag[9], 0);
      pflag[i] = 1;
      rec1(0);
    }
    cout << num << endl;
  }
  return 0;
}