surface = gets.split.map(&:to_i)
throw = {E: [3, 2, 6, 1, 5, 4], N: [5, 1, 3, 4, 6, 2],
         S: [2, 6, 3, 4, 1, 5], W: [4, 2, 1, 6, 5, 3]}
th = {}
throw.each_key {|k| th[k] = throw[k].map(&:pred)}
gets.to_i.times do
  top, front = gets.split.map(&:to_i).map {|x| surface.find_index(x)}
  stack = []
  dice = [*0..5]
  loop do
    [:E, :N, :S, :W].each {|d| stack << [dice, d]}
    dice, dir = stack.shift
    next_dice = Array.new(6, 0)
    dice.each_with_index do |d, i|
      next_dice[th[dir][i]] = d
    end
    if next_dice[0] == top and next_dice[1] == front
      puts surface[next_dice[2]]
      break
    else
      dice = next_dice
    end
  end
end

