type Dice = [Pip]
type Pip = String
type ToBe = [Pip]

main :: IO ()
main = do
    dice <- fmap words getLine
    _    <- getLine
    questions <- fmap (map words . lines) getContents
    putStr . unlines $ map (rightSide dice) questions

rightSide :: Dice -> ToBe -> Pip
rightSide dice tobe =
    head [s | s@(a:b:_) <- allStates dice, [a,b] == tobe] !! 2

allStates :: Dice -> [Dice]
allStates = concatMap allTurns . allTops

allTops :: Dice -> [Dice]
allTops dice = scanl role dice "EEENN"

allTurns :: Dice -> [Dice]
allTurns dice = scanl turn dice "RRR"

turn :: Dice -> Char -> Dice
turn [a,b,c,d,e,f] com
    | com == 'R' = [a,c,e,b,d,f]
    | com == 'L' = [a,d,b,e,c,f]

role :: Dice -> Char -> Dice
role [a,b,c,d,e,f] com
    | com == 'E' = [d,b,a,f,e,c]
    | com == 'N' = [b,f,c,d,a,e]
    | com == 'S' = [e,a,c,d,f,b]
    | com == 'W' = [c,b,f,a,e,d]
