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

main :: IO ()
main = do
  ls <- words <$> getLine
  q  <- readLn
  fs <- map words <$> replicateM q getLine
  let ds = makeDice ("_" : ls)
  putStr $ unlines $ map (solve ds) fs

type Dice = [(String, [String])]

makeDice :: [String] -> Dice
makeDice ls = map f di
  where
    f (x, ys) = (ls !! x, map (ls !!) ys)
    di = [(1, [5, 3, 2, 4]),
          (2, [1, 3, 6, 4]),
          (3, [1, 5, 6, 2]),
          (4, [1, 2, 6, 5]),
          (5, [1, 4, 6, 3]),
          (6, [2, 3, 5, 4])]

solve :: Dice -> [String] -> String
solve ds [t, f] = ys !! (if topIndex == 0 then 3 else topIndex - 1)
  where
    ys = fromJust $ lookup t ds
    topIndex = fromJust $ elemIndex f ys

