### constant

N = 3
NN = N * N

MCHARS = [['R', 'r'], ['G', 'g'], ['B', 'b'], ['W', 'w']]

### subroutines

def count_rec(x, y)
  return 1 if y >= N

  x0 = x + 1
  y0 = y
  if x0 >= N
    x0 = 0
    y0 += 1
  end

  id = y * N + x
  count = 0

  for i in (0...NN)
    next if $used[i]

    for pc in $pcs[i]
      next if x > 0 && $matches[$grds[id - 1][1]][pc[3]].nil?
      next if y > 0 && $matches[$grds[id - N][2]][pc[0]].nil?

      $used[i] = true
      $grds[id] = pc

      count += count_rec(x0, y0)

      $used[i] = false
    end
  end

  count
end

### main

$matches = {}
for mch in MCHARS
  c0, c1 = mch
  $matches[c0] = {c1 => true}
  $matches[c1] = {c0 => true}
end

n = gets.to_i

n.times do
  $pcs = []
  for pc in gets.strip.split
    pns = pc.split('')
    rots = [pns.clone]
    for i in (1..3)
      rots << pns.rotate(i)
    end
    $pcs << rots
  end
  #p $pcs

  $used = NN.times.map{false}
  $grds = []

  c = count_rec(0, 0)
  puts c
end