import Control.Monad
import Control.Applicative

data Op = S | E | N | W deriving (Show, Eq, Enum)

fromChar c | c == 'S' = S
           | c == 'E' = E
           | c == 'N' = N
           | c == 'W' = W
           | otherwise = error $ "Invalid char " ++ [c]

fromString [] = []
fromString (x:xs) = (fromChar x):(fromString xs)

data Dice = Dice { top :: Int, front :: Int, right :: Int, left :: Int, back :: Int, bottom :: Int }
  deriving (Show, Eq)

makeDice [t, f, r, l, ba, bo] = Dice t f r l ba bo

roll (Dice t f r l ba bo) S = Dice ba t r l bo f
roll (Dice t f r l ba bo) N = Dice f bo r l t ba
roll (Dice t f r l ba bo) E = Dice l f t bo ba r
roll (Dice t f r l ba bo) W = Dice r f bo t ba l
rolls = foldl roll

guessRight :: Int -> Int -> Dice -> Int
guessRight t f dice = right . head . filter (\x -> front x == f) $ scanl rolls d [[N, E, S], [N, E, S], [N, E, S]]
  where d = head . filter (\x -> top x == t) $ scanl roll dice [N, E, N, E, N]

solve :: [Int] -> [Int] -> Int
solve lbls [t,f] = guessRight t f $ makeDice lbls

main = do
  labels <- map read . words <$> getLine :: IO [Int]
  n <- readLn :: IO Int
  xs <- map (map read . words) <$> replicateM n getLine :: IO [[Int]]
  mapM_ (print . solve labels) xs


