class Dice
  attr_reader :top, :bottom, :front, :back, :left, :right #dice.top????????§???????????????????????????

  def initialize(dice_numbers) #dice????????????????¨??????????
    @top = dice_numbers[0]
    @back = dice_numbers[1]
    @right = dice_numbers[2]
    @left = dice_numbers[3]
    @front = dice_numbers[4]
    @bottom = dice_numbers[5]
  end

  def roll_dice(order_top, order_back)
      while true
        execute_order(["N", "W"].sample)
      break if @top == order_top && @back == order_back
    end
    # p "[#{@top}, #{@back}, #{@right}, #{@left}, #{@front}, #{@bottom}]"
  end

  # p "[#{top}, #{back}, #{right}, #{left}, #{front}, #{bottom}]"
  private #class?????§????????????????????????
  def move_north
    tmp = @top
    @top = @back
    @back = @bottom
    @bottom = @front
    @front = tmp
  end

  def move_west
    tmp = @top
    @top = @right
    @right = @bottom
    @bottom = @left
    @left = tmp
  end

  def execute_order(orders) #???????????????????????§????????????????????????????????????
      orders.each do |order| #??????????????????????????¨????????????????????¢?????????
        if order == "N"
          move_north
        elsif order == "S" #3???north?????¢????????¨1???south?????¢??????????????¨????????¶????????????
          3.times do
            move_north
          end
        elsif order == "W"
          move_west
        elsif order == "E" #3???east?????¢????????¨1???west?????¢??????????????¨????????¶????????????
          3.times do
            move_west
          end
        else
          puts "error!" #?????¬????????????????????????
        end
      end
  end
end

dice_numbers = gets.chomp.split.map(&:to_i)
n = gets.chomp.to_i
answers = []
n.times do
  orders = gets.chomp.split.map(&:to_i)
  dice = Dice.new(dice_numbers)
  dice.roll_dice(orders[0], orders[1])
  answers << dice.right
end
answers.each do |answer|
  puts answer
end