class Dice
	def initialize(n=Array.new(6,0))
		@num = n
		@position = { u: @num[0],
									d: @num[5],
									r: @num[2],
									l: @num[3],
									f: @num[4],
									b: @num[1]
		}
		@LRRoll = [:u, :b, :d, :f]
		@FBRoll = [:u, :l, :d, :r]
		@UDRoll = [:f, :l, :b, :r]
	end

	def v_up
		@position[:u]
	end

	def v_down
		@position[:d]
	end

	def v_foward
		@position[:f]
	end

	def v_back
		@position[:b]
	end

	def v_right
		@position[:r]
	end

	def v_left
		@position[:l]
	end

	def move(v)
		case v
		when "N"
			Roll(@LRRoll)
		when "S"
			Roll(@LRRoll, -1)
		when "E"
			Roll(@FBRoll)
		when "W"
			Roll(@FBRoll, -1)
		end
	end

	def move_roll(n)
		n.times { Roll(@UDRoll) }
	end

	def Roll(roll, v=1)
		v = -1 if v != 1
		t = @position[roll[0]]
		@position[roll[  0]] = @position[roll[1*v]]
		@position[roll[1*v]] = @position[roll[2*v]]
		@position[roll[2*v]] = @position[roll[3*v]]
		@position[roll[3*v]] = t
	end

end

n = gets.chomp.split(" ").map &:to_i
d = Dice.new(n)

gets.to_i.times {
	d = gets.chomp.split(" ").map &:to_i
	up, back = d[0], d[1]

	case up
	when Dice.v_down
		2.times { Dice.move("N") }
	when Dice.v_right
		Dice.move("W")
	when Dice.v_left
		Dice.move("E")
	when Dice.v_foward
		Dice.move("S")
	when Dice.v_back
		Dice.move("N")
	end

  case back
	when Dice.v_left
		Dice.move_roll(1)
	when Dice.v_foward
		Dice.move_roll(2)
	when Dice.v_right
		Dice.move_roll(3)
	end

	p Dice.v_right
}