class Dice
  attr_accessor :face
  Edge = [[0,1,2],[0,2,4],[0,4,3],[0,3,1],[1,5,2],[2,5,4],[4,5,3],[3,5,1]]
  def query(a, b)
    x,y = @face.index(a),@face.index(b)
    Edge.each do |item|
      if i = item.index(y)
        if item[i-1] == x
          return item[i-2]
        end
      end
    end
  end
end

dice = Dice.new
dice.face = gets.chomp.split.map(&:to_i)
gets.chomp.to_i.times do
  a,b = gets.chomp.split.map(&:to_i)
  puts dice.face[dice.query(a, b)]
end