class Die
  def initialize(men)
    @t, @s, @e, @w, @n, @b = men
  end

  def top; @t; end
  def right; @e; end

  def move(d)
    case d
    when 'N'
      @t, @s, @b, @n = @s, @b, @n, @t
    when 'W'
      @w, @b, @e, @t = @t, @w, @b, @e
    when 'E'
      @w, @b, @e, @t = @b, @e, @t, @w
    when 'S'
      @t, @s, @b, @n = @n, @t, @s, @b
    end
  end

  def make_top(num)
    3.times do
      move 'N'
      return if @t == num
      move 'E'
      return if @t == num
    end
    raise "#{num} is not found"
  end

  def rotate
    @n, @w, @s, @e = @w, @s, @e, @n
  end

  def rotate_until(num)
    4.times do
      rotate
      return if @s == num
    end
    raise "#{num} is not found"
  end
end

d = Die.new(gets.split)
gets.to_i.times do
  top, front = gets.split
  d.make_top(top)
  d.rotate_until(front)
  puts d.right
end

