dice = STDIN.gets.chomp().split(" ")
# p dice
q = STDIN.gets.to_i
newdice = []
for i in 0..q-1 do
  line = STDIN.gets.split(" ")
  d = [line[0], line[1]]
  newdice << d
end
# p newdice
result = Array.new(q){|i| Array.new(6)}
# p result

def move(direction, dice)
  case direction
  when "S" then
    temp = dice[0]
    dice[0] = dice[4]
    dice[4] = dice[5]
    dice[5] = dice[1]
    dice[1] = temp
  when "N" then
    temp = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = temp
  when "E" then
    temp = dice[0]
    dice[0] = dice[3]
    dice[3] = dice[5]
    dice[5] = dice[2]
    dice[2] = temp
  when "W" then
    temp = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = temp
  end
  return dice
end

def getDrection(ran)
  case ran
  when 0
    return "S"
  when 1
    return "N"
  when 2
    return "E"
  when 3
    return "W"
  end
end

while true
  direction = getDrection(rand(4))
  dice = move(direction, dice)
  # p "dice = #{dice}"
  # p "newdice = #{newdice}"
  count = 0
  newdice.each do |x|
    # p "x[0] = #{x[0]}"
    # p "dice[0] = #{dice[0]}"
    # p "x[1] = #{x[1]}"
    # p "dice[1] = #{dice[1]}"
    if x[0] == dice[0] && x[1] == dice[1] then
      result[count][2] = dice[2]
    end
    count += 1
  end
  flag = true
  # puts "result = #{result}"
  result.each do |x|
    if x[2] == nil then flag = false end
  end
  if flag == true then break end
end

result.each do |x|
  puts x[2]
end