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

data Dice = Dice{tp::Int, fr::Int, rt::Int, lt::Int, bk::Int, bm::Int} deriving (Show, Eq)

roll :: Dice -> Char -> Dice
roll (Dice tp fr rt lt bk bm) dir
  | dir == 'N' = Dice fr bm rt lt tp bk
  | dir == 'S' = Dice bk tp rt lt bm fr
  | dir == 'E' = Dice lt fr tp bm bk rt
  | dir == 'W' = Dice rt fr bm tp bk lt
  | otherwise = Dice tp fr rt lt bk bm

rolls :: Dice -> String -> Dice
rolls d s = foldl roll d s

initDice :: [Int] -> Dice
initDice [tp, fr, rt, lt, bk, bm] = Dice tp fr rt lt bk bm

setDice :: Dice -> Int -> Int -> Dice
setDice d t f = d2
  where
    d1 = head . filter (\x -> tp x == t) $ scanl roll d "NENEN"
    d2 = head . filter (\x -> fr x == f) $ scanl rolls d1 ["NES","NES","NES"]

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

solve :: [Int] -> [[Int]] -> [Int]
solve xs ys = map rt $ map (f (setDice d)) ys
  where d = initDice xs
        f g [i, j] = g i j