class Dice
	attr_accessor :t, :b, :n, :e, :s, :w
	def initialize(*labels)
		@t = labels[0]
		@s = labels[1]
		@e = labels[2]
		@w = labels[3]
		@n = labels[4]
		@b = labels[5]
	end
	def roll(dir)
		case dir
		when 'N'
			@t, @s, @b, @n = @s, @b, @n, @t
		when 'S'
			@s, @b, @n, @t = @t, @s, @b, @n
		when 'W'
			@t, @e, @b, @w = @e, @b, @w, @t
		when 'E'
			@e, @b, @w, @t = @t, @e, @b, @w
		end
	end
end

dice = Dice.new(*gets.split)
gets.to_i.times{
	t, s = gets.split
	8.times{|i|
		break if dice.s == s
		dice.roll 'NW'[i%4/3]
	}
	dice.roll 'W' while dice.t != t
	puts dice.e
}