class Dice
  attr :top, :front, :right, :left, :back, :bottom
  def initialize a,b,c,d,e,f
    @top = a; @front = b; @right = c; @left = d; @back = e; @bottom = f
  end
  def rotN
    t = @top; @top = @front; @front = @bottom; @bottom = @back; @back = t
  end
  def rotE
    t = @top; @top = @left; @left = @bottom; @bottom = @right; @right = t
  end
  def rotW
    t = @top; @top = @right; @right = @bottom; @bottom = @left; @left = t
  end
  def rotS
    t = @top; @top = @back; @back = @bottom; @bottom = @front; @front = t
  end
  def rotV
    t = @front; @front = @left; @left = @back; @back = @right; @right = t
  end
end

a,b,c,d,e,f = gets.split.map {|s| s.to_i}
die = Dice.new(a,b,c,d,e,f)
n = gets.to_i
for i in 1..n
  t,f = gets.split.map {|s| s.to_i}
  if die.right == t
    die.rotW
  elsif die.left == t
    die.rotE
  else
    until die.top == t
      die.rotN
    end
  end
  until die.front == f
    die.rotV
  end
  puts die.right
end