class Dice
	def initialize(labels)
		@labels = labels
		@top    = labels[0]
		@front  = labels[1]
		@right  = labels[2]
		@left   = labels[3]
		@back   = labels[4]
		@bottom = labels[5]
	end
	attr_reader :top, :front, :right

	def roll(op)
		case op
		when "E"
			@t      = @top
			@top    = @left
			@left   = @bottom
			@bottom = @right
			@right  = @t
		when "W"
			@t      = @top
			@top    = @right
			@right  = @bottom
			@bottom = @left
			@left   = @t
		when "N"
			@t      = @top
			@top    = @front
			@front  = @bottom
			@bottom = @back
			@back   = @t
		when "S"
			@t      = @top
			@top    = @back
			@back   = @bottom
			@bottom = @front
			@front  = @t
		when "R" # Roll around front and back
			@f      = @front
			@front  = @left
			@left   = @back
			@back   = @right
			@right  = @f 
		end
	end
end
labels = gets.strip.split(" ").map{|i| i.to_i}
dice = Dice.new(labels)
n = gets.strip.to_i
n.times do
	inputs = gets.strip.split(" ").map{|i| i.to_i}
	top, front = inputs[0], inputs[1]
	count = 0
	until dice.top == top
		dice.roll("E")
		count+=1
		if count > 3
			count = 0
			dice.roll("N")
		end
	end
	count = 0
	until dice.front == front
		dice.roll("R")
		count+=1
	end
	puts dice.right
end

