class Dice
	def initialize()
		@number = gets.split.map(&:to_i)

		# top bottom N E S W
		@dice_number = [1, 6, 5, 3, 2, 4]
	end

	def rotate(command)
		swap_number = []

		case command
		when 'N'
			swap_number = [2, 1, 4]
			swap(swap_number)
		when 'E'
			swap_number = [3, 1, 5]
			swap(swap_number)
		when 'S'
			swap_number = [4, 1, 2]
			swap(swap_number)
		when 'W'
			swap_number = [5, 1, 3]
			swap(swap_number)
		end
	end

	def rotate_horizontal 
		tmp = @dice_number[2]
		(2..4).each {|i| @dice_number[i] = @dice_number[i+1]}
		@dice_number[5] = tmp
	end

	def swap(number)
		number.map do |n|
			@dice_number[0], @dice_number[n] = @dice_number[n], @dice_number[0]
		end
	end

	def top
		@number[@dice_number[0]-1]
	end

	def front
		@number[@dice_number[4]-1]
	end

	def right
		@number[@dice_number[3]-1]
	end
end

dice = Dice.new

gets.to_i.times do
	top, front = gets.split.map(&:to_i)

	while top != dice.top do
		rotate_command = ['N', 'E', 'S', 'W'].sample
		dice.rotate(rotate_command)
	end

	while front != dice.front do
		dice.rotate_horizontal
	end

	puts dice.right
end