#!/usr/bin/env ruby

class Dice
	attr_reader :top, :s, :e

    def initialize(top, s, e, w, n, bottom)
        @top    = top
        @s      = s
        @e      = e
        @w      = w
        @n      = n
        @bottom = bottom
	end

	def rotate!(dist)
		@top, @s, @e, @w, @n, @bottom = 
		case dist
		when 'N' then [@s, @bottom, @e, @w, @top, @n]
		when 'S' then [@n, @top, @e, @w, @bottom, @s]
		when 'E' then [@w, @s, @top, @bottom, @n, @e]
		when 'W' then [@e, @s, @bottom, @top, @n, @w]
		end
	end
end

if __FILE__ == $0 then
	dice = Dice.new(*gets.split.map(&:to_i))

	ref = {}
	while ref.size < 24 do
		ref[[dice.top, dice.s].to_s] = dice.e
		dice.rotate!('NSEW'[rand(4)])
	end

	gets.chomp.to_i.times do
		top, s = gets.split.map(&:to_i)
		p ref[[top, s].to_s]
	end
end

