require 'set'

roll = lambda do |dice, p|
  dice.each_with_index.sort_by {|_, i| p[i] }.map(&:first)
end
permutations = Set.new
permutations << [0, 1, 2, 3, 4, 5]
todo = [[0, 1, 2, 3, 4, 5]]
until todo.empty?
  dice = todo.pop
  [[2, 1, 5, 0, 4, 3], [4, 0, 2, 3, 5, 1]].each do |p|
    d = roll[dice, p]
    unless permutations.member? d
      permutations << d
      todo << d
    end
  end
end
dice = gets.split.map(&:to_i)
gets.to_i.times do
  a, b = gets.split.map {|s| dice.find_index s.to_i }
  p dice[permutations.find {|p, q| a == p && b == q }[2]]
end