import Control.Monad
import Control.Applicative

move [a,b,c,d,e,f] o
	| o == 'E' = [d,b,a,f,e,c]
	| o == 'N' = [b,f,c,d,a,e]
	| o == 'S' = [e,a,c,d,f,b]
	| o == 'W' = [c,b,f,a,e,d]

solve dice@[a,b,c,d,e,f] q@[a',b'] (x:xs)
	| a == a' && b == b' = c
	| otherwise = solve (move dice x) q xs

main = do
	dice <- words <$> getLine
	n <- readLn
	replicateM_ n $ do
		qs <- words <$> getLine
		putStrLn $ solve dice qs (cycle "EEENNWWS")