$dice = gets.split(" ").map(&:to_i)

def get_right(tt)
  c = [[0,3,5,2,4,0],[4,0,1,6,0,3],[2,6,0,0,1,5],[5,1,0,0,6,2],[3,0,6,1,0,4],[0,4,2,5,3,0]]
  t = $dice.find_index(tt[0])
  f = $dice.find_index(tt[1])
  puts $dice[c[t][f]-1]
end

(gets.to_i).times do
  get_right(gets.split(" ").map(&:to_i))
end

