class Dice
	def initialize(array)
		@data = array
	end
	
	def move(dir)
		case dir.downcase
		when "e"
			tmp=@data[0]
			@data[0]=@data[3]
			@data[3]=@data[5]
			@data[5]=@data[2]
			@data[2]=tmp
		when "n"
			tmp=@data[0]
			@data[0]=@data[1]
			@data[1]=@data[5]
			@data[5]=@data[4]
			@data[4]=tmp
		when "s"
			tmp=@data[0]
			@data[0]=@data[4]
			@data[4]=@data[5]
			@data[5]=@data[1]
			@data[1]=tmp
		when "w"
			tmp=@data[0]
			@data[0]=@data[2]
			@data[2]=@data[5]
			@data[5]=@data[3]
			@data[3]=tmp
		end
	end
	
	def rotate
		tmp=@data[1]
		@data[1]=@data[2]
		@data[2]=@data[4]
		@data[4]=@data[3]
		@data[3]=tmp
	end
	
	def top
		return @data[0]
	end
	
	def front
		return @data[1]
	end
	
	def left
		return @data[3]
	end
	
	def right
		return @data[2]
	end
	
	def back
		return @data[4]
	end
	
	def bottom
		return @data[5]
	end
end

array = gets.chomp.split(" ").map(&:to_i)
dice = Dice.new(array)
q = gets.chomp.to_i

q.times{
    data = gets.chomp.split(" ").map(&:to_i)
	if dice.top == data[0]
	elsif dice.front == data[0]
		dice.move("N")
	elsif dice.left == data[0]
		dice.move("E")
	elsif dice.right == data[0]
		dice.move("W")
	elsif dice.back == data[0]
		dice.move("S")
	elsif dice.bottom == data[0]
		dice.move("N")
		dice.move("N")
	end
	
	while dice.front != data[1]
		dice.rotate
	end
	
	puts dice.right
}