class Dice
	def initialize(array)
		@a = Array.new(array)
	end
	
	def rotate(direction)
		if direction == "N" then
			tmp = @a[0]
			@a[0] = @a[1]
			@a[1] = @a[5]
			@a[5] = @a[4]
			@a[4] = tmp
		elsif direction == "S" then
			tmp = @a[0]
			@a[0] = @a[4]
			@a[4] = @a[5]
			@a[5] = @a[1]
			@a[1] = tmp
		elsif direction == "E" then
			tmp = @a[0]
			@a[0] = @a[3]
			@a[3] = @a[5]
			@a[5] = @a[2]
			@a[2] = tmp
		elsif direction == "W" then
			tmp = @a[0]
			@a[0] = @a[2]
			@a[2] = @a[5]
			@a[5] = @a[3]
			@a[3] = tmp
		end
	end
	
	def get_top
		@a[0]
	end
	
	def get_right
		@a[2]
	end
	
	def get_front
		@a[1]
	end
	
	def get_left
		@a[3]
	end
	
	def get_bottom
		@a[5]
	end
	
	def get_rear
		@a[4]
	end
end

def array_to_i(a)
	0.upto(a.length) {|i|
		a[i] = a[i].to_i
	}
	return a
end

a = STDIN.gets.chop.split(" ")
a = array_to_i(a)
dice = Dice.new(a)

q_num = STDIN.gets.to_i

0.upto(q_num - 1) {
	q = STDIN.gets.chomp.split(" ")
	q = array_to_i(q)
	
	if dice.get_left == q[1] then
		dice.rotate("E")
		dice.rotate("S")		
	elsif dice.get_right == q[1] then
		dice.rotate("W")
		dice.rotate("S")
	else
		until dice.get_front == q[1] do
			dice.rotate("S")
		end
	end
	
	until dice.get_top == q[0] do
		dice.rotate("E")
	end
	
	print dice.get_right, "\n"
}
