faces = gets.split
q = gets.to_i

# right_side_finder
rsf = [[1, 2, 4, 3], [0, 3, 5, 2], [0, 1, 5, 4]]
3.upto(5) {|i| rsf[i] = rsf[5 - i].reverse }

q.times do
	top, front = gets.split.map{|i| faces.index(i)}

	side_seq = rsf[top]
	index = side_seq.index(front)

	puts faces[side_seq[(index + 1) % 4]]
end