import Control.Applicative
import Control.Monad
import Data.List

next x (a:b:as)|x==a = b
next x (a:as) = next x as

eTS :: Int -> Int -> Int
eTS t s = next s fromT
          where fromT = [[1,2,4,3,1],[0,3,5,2,0],[0,1,5,4,0],[0,4,5,1,0],[0,2,5,3,0],[4,2,1,3,4]]!!t

-- TSEWNB
main = do
    label <- words <$> getLine
    q <- readLn
    m <- replicateM q ( (map (\ w -> let (Just a) = findIndex(== w) label in a).words) <$> getLine)
    let a = map (\x -> eTS (x!!0) (x!!1)) m
    putStrLn $ (intercalate "\n".map (label!!)) a