# ITP1_11_B: Dice2

class Dice
  TOP    = 0
  FRONT  = 1
  RIGHT  = 2
  LEFT   = 3
  BACK   = 4
  BOTTOM = 5

  def initialize(labels)
    @labels = labels
  end

  def pip_of(side)
    @labels[side]
  end
 
  def rotate(p1, p2, p3, p4)
    l = @labels
    l[p1], l[p2], l[p3], l[p4] = l[p4], l[p1], l[p2], l[p3]
  end
 
  def roll_to(direction)
    rotate *case direction
    when 'N'
      [TOP, FRONT, BOTTOM, BACK]
    when 'E'
      [TOP, LEFT, BOTTOM, RIGHT]
    when 'S'
      [TOP, BACK, BOTTOM, FRONT]
    when 'W'
      [TOP, RIGHT, BOTTOM, LEFT]
    end
  end
end
 
labels     = gets.split.map &:to_i
n_changes  = gets.to_i
directions = %w(N E S W)
dice       = Dice.new labels

n_changes.times do
  top, front = gets.split.map &:to_i
  while true
    dice.roll_to directions.sample
    if dice.pip_of(Dice::TOP) == top && dice.pip_of(Dice::FRONT) == front
      puts dice.pip_of(Dice::RIGHT)
      break
    end
  end
end