class Dice
  attr_reader :top, :front, :right
  def initialize(input_str)
    numbers = input_str.chomp.split(' ')
    @top = numbers[0]
    @front = numbers[1]
    @right = numbers[2]
    @back = numbers[4]
    @left = numbers[3]
    @buttom = numbers[5]
  end

  def move(direction)
    send('move_to_' + direction)
  end

  def move_to_E
    tmp = @top
    @top = @left
    @left = @buttom
    @buttom = @right
    @right = tmp
  end

  def move_to_W
    tmp = @top
    @top = @right
    @right = @buttom
    @buttom = @left
    @left = tmp
  end

  def move_to_N
    tmp = @top
    @top = @front
    @front = @buttom
    @buttom = @back
    @back = tmp
  end

  def move_to_S
    tmp = @top
    @top = @back
    @back = @buttom
    @buttom = @front
    @front = tmp
  end

  def match?(hash)
    hash.each { |attr_name, val| return false if send(attr_name) != val }
    true
  end
end

def get_dice_right(dice, arr)
  4.times do
    return dice.right if dice.match?(top: arr[0], front: arr[1])
    dice.move('S')
  end
  dice.move('W')
  return dice.right if dice.match?(top: arr[0], front: arr[1])
  dice.move('EE')
  return dice.right
end

dice = Dice.new(gets)
question = gets.chomp.to_i
question.times { |i| puts get_dice_right(dice, gets.chomp.split(' ')) }