table = {
  0 => [1, 3],
  1 => [5, 2],
  2 => [4, 0],
  3 => [5, 1],
  4 => [0, 3],
  5 => [4, 2]
}

dice = gets.split
gets.to_i.times do 
  top, front = gets.split.map {|x| dice.index(x) }
  side = 3 - [top, 5 - top].min - [front, 5 - front].min
  puts dice[table[top].include?(front) ? side : 5 - side]
end