class Die
  attr_accessor :front, :back, :top, :bottom, :left, :right
  def initialize
  @front, @back, @top, @bottom, @left, @right = 6.times.map{0}
  end
  def check?
    return false if [@front, @back, @top, @bottom, @left, @right].any?(&:zero?)
    return false unless [@front + @back, @left + @right, @top + @bottom].all?{|v| v == 7}
    true
  end
  def rotate(direction)
    case direction
      when :top
        @top, @front, @bottom, @back = @front, @bottom, @back, @top
      when :bottom
        @top, @front, @bottom, @back = @back, @top, @front, @bottom
      when :clockwise
        @left, @front, @right, @back = @front, @right, @back, @left
      when :counterclockwise
        @left, @front, @right, @back = @back, @left, @front, @right
    end
  end
end

@net = Array.new
@die = Die.new
def dfs(i, j)
  if (0 < j) && (@net[i][j - 1] != 0)
    @die.rotate(:counterclockwise)
    @die.front = @net[i][j - 1]
    @net[i][j - 1] = 0
    dfs(i, j - 1)
    @die.rotate(:clockwise)
  end
  if (j + 1 < 5) && (@net[i][j + 1] != 0)
    @die.rotate(:clockwise)
    @die.front = @net[i][j + 1]
    @net[i][j + 1] = 0
    dfs(i, j + 1)
    @die.rotate(:counterclockwise)
  end
  if (i + 1 < 5) && (@net[i + 1][j] != 0)
    @die.rotate(:top)
    @die.front = @net[i + 1][j]
    @net[i + 1][j] = 0
    dfs(i + 1, j)
    @die.rotate(:bottom)
  end
end

gets.to_i.times do
  @die = Die.new
  @net = Array.new
  count = Hash.new{0}
  5.times do @net << gets.split.map(&:to_i) end
  5.times do |i|
    5.times do |j|
      v = @net[i][j]
      next if v.zero?
      count[v] += 1
    end
  end
  unless (1..6).all?{|n| count[n] == 1} # 1-6が不揃い
    puts "false"
    next
  end
  5.times do |i|
    5.times do |j|
      next if @net[i][j].zero?
      @die.front = @net[i][j]
      @net[i][j] = 0
      dfs(i, j)
    end
  end
  puts @die.check? ? "true" : "false"
end