class Dice
	attr_reader :top, :right, :front

  def initialize(ary)
    @top, @front, @right, @left, @back, @bottom = ary
  end

  def roll(way)
    case way.downcase
    when 'e'
      tmp = @top
      @top = @left
			@left = @bottom
			@bottom = @right
			@right = tmp
    when 'w'
			tmp = @top
			@top = @right
			@right = @bottom
			@bottom = @left
			@left = tmp
    when 's'
			tmp = @top
			@top = @back
			@back = @bottom
			@bottom = @front
			@front = tmp
    when 'n'
			tmp = @top
			@top = @front
			@front = @bottom
			@bottom = @back
			@back = tmp
    end
  end
end

dice = Dice.new(gets.chomp.split(/\s/).map(&:to_i))
q = gets.chomp.to_i
q.times do
  top, front = gets.chomp.split(/\s/).map(&:to_i)
  way = ['N', 'E', 'S', 'W']
  loop do
    dice.roll(way.sample)
		if dice.top == top && dice.front == front
      puts dice.right
			break
		end
  end
end