import Control.Applicative ((<$>))
import Control.Monad (replicateM)

main :: IO ()
main = do
    d <- readDice <$> getLine
    n <- readLn
    ps <- replicateM n $ do
        [t,s] <- words <$> getLine
        return (t,s)
    mapM_ (putStrLn . solve d) ps

solve :: Dice -> (String,String) -> String
solve d (t',s') = east $ matchS s' $ matchT t' d

matchT :: String -> Dice -> Dice
matchT t' d@(Dice t s e w n b)
    | t' == t = d
    | t' == s = goN d
    | t' == e = goW d
    | t' == w = goE d
    | t' == n = goS d
    | t' == b = goN $ goN d

matchS :: String -> Dice -> Dice
matchS s' d@(Dice t s e w n b)
    | s' == s = d
    | s' == e = rot . rot . rot $ d
    | s' == w = rot d
    | s' == n = rot . rot $ d

goE :: Dice -> Dice
goE (Dice t s e w n b) = Dice w s t b n e
goN :: Dice -> Dice
goN (Dice t s e w n b) = Dice s b e w t n
goS :: Dice -> Dice
goS (Dice t s e w n b) = Dice n t e w b s
goW :: Dice -> Dice
goW (Dice t s e w n b) = Dice e s b t n w

rot :: Dice -> Dice
rot (Dice t s e w n b) = Dice t w s n e b

data Dice = Dice
    { top :: String
    , south :: String
    , east :: String
    , west :: String
    , north :: String
    , bottom :: String
    }

readDice :: String -> Dice
readDice str = Dice t s e w n b
    where (t:s:e:w:n:b:_) = words str