### constant

NS = 5

DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

XM = 0
XP = 1
YM = 2
YP = 3
ZM = 4
ZP = 5

### subroutines

def check_dice(sts)
  sx = sy = 0
  while sts[sy][sx] == 0
    sx += 1
    if sx >= NS
      sx = 0
      sy += 1
      return false if sy >= NS
    end
  end
  #p [sx, sy]

  folded = NS.times.map{NS.times.map{false}}
  folded[sy][sx] = true
  sides = []
  sides[ZM] = sts[sy][sx]
  q = [[sx, sy, ZM, [XP, YM, XM, YP]]]

  while ! q.empty?
    x, y, sd, edges = q.shift

    for d in (0..3)
      x0 = x + DXYS[d][0]
      y0 = y + DXYS[d][1]
      if x0 >= 0 && x0 < NS && y0 >= 0 && y0 < NS &&
          sts[y0][x0] != 0 && ! folded[y0][x0]
        folded[y0][x0] = true

        d2 = (d + 2) % 4
        sd2 = (sd ^ 1)

        sd0 = edges[d]
        edges0 = edges.clone
        edges0[d] = sd2
        edges0[d2] = sd

        return false if ! sides[sd0].nil?
        sides[sd0] = sts[y0][x0]

        sd1 = (sd0 ^ 1)
        return false if ! sides[sd1].nil? && sides[sd0] + sides[sd1] != 7

        q << [x0, y0, sd0, edges0]
      end
    end
  end

  for i in (0..5)
    return false if sides[i].nil?
  end

  true
end

### main

n = gets.to_i

n.times do
  sts = []

  sn = 0
  dcnts = [0] * 7
  invalid = false

  NS.times do
    ds = gets.split.map(&:to_i)
    sts << ds

    for d in ds
      next if d == 0
      sn += 1
      invalid = true if dcnts[d] > 0
      dcnts[d] += 1
    end
  end
  #p sts
  #p [invalid, dcnts]

  if invalid || sn != 6
    puts 'false'
    next
  end

  puts (check_dice(sts) ? 'true' : 'false')
end