module Main where
--import Debug.Trace
import Data.List
import Data.Char
import Data.Array
import Data.Maybe
import Data.Ord
import Control.Monad

solve :: [Int] -> [[Int]] -> [Int]
solve pl = iter where
    iter [] = []
    iter ([t, f]:qs) = search ! 2 : iter qs where
        dice = listArray (0, 5) pl
        search = rollUntil 0 t "W" $ case dice of
            _   | dice!3 == f  -> roll "ES" dice
                | dice!2 == f  -> roll "WS" dice
                | otherwise -> rollUntil 1 f "S" dice
        roll [] d = d
        roll (c:cs) d = roll cs $ case c of
            'N' -> d // [(0, d!1), (1, d!5), (4, d!0), (5, d!4)]
            'E' -> d // [(0, d!3), (2, d!0), (3, d!5), (5, d!2)]
            'W' -> d // [(0, d!2), (2, d!5), (3, d!0), (5, d!3)]
            'S' -> d // [(0, d!4), (1, d!0), (4, d!5), (5, d!1)]
        rollUntil p x c = iter where
            iter d  | d!p == x = d
                    | otherwise = iter (roll c d)
                                
readLine:: IO [Int]
readLine = fmap (map read . words) getLine

main :: IO() 
main = do
    pl <- readLine
    n  <- readLn
    ql <- replicateM n readLine
--    print pl
--    print ql
    mapM_ print (solve pl ql)

