import           Control.Applicative
import           Control.Monad
import           Data.List
import           Data.Maybe          (fromJust)

newtype Dice =
    Dice [Int]
    deriving (Eq, Show)

dice :: Dice
dice = Dice [0,1,2,3,4,5]

roll :: Char -> Dice -> Dice
roll 'E' (Dice ds) = Dice [ds !! (i-1) | i <- [4,2,1,6,5,3]]
roll 'N' (Dice ds) = Dice [ds !! (i-1) | i <- [2,6,3,4,1,5]]
roll 'S' (Dice ds) = Dice [ds !! (i-1) | i <- [5,1,3,4,6,2]]
roll 'W' (Dice ds) = Dice [ds !! (i-1) | i <- [3,2,6,1,5,4]]

rollH :: Dice -> Dice
rollH (Dice ds) = Dice [ds !! (i-1) | i <- [1,4,2,5,3,6]]

rollTop :: Int -> Dice -> Dice
rollTop 0 ds = ds
rollTop 1 ds = roll 'N' ds
rollTop 2 ds = roll 'W' ds
rollTop 3 ds = roll 'E' ds
rollTop 4 ds = roll 'S' ds
rollTop 5 ds = roll 'S' $ roll 'S' ds

getRight :: Int -> Int -> Int
getRight top front =
    (\(Dice d) -> d !! 2) . head $
    filter (\(Dice (x:y:_)) -> x == top && y == front) hs
  where
    d = rollTop top dice
    hs = take 4 $ iterate rollH d

main :: IO ()
main = do
    xs <- fmap read . words <$> getLine :: IO [Int]
    getLine
    yss <- fmap (fmap read . words) . lines <$> getContents :: IO [[Int]]
    forM_ yss $ \[a, b] -> do
        let (Just top, Just front) = (a `elemIndex` xs, b `elemIndex` xs)
        let right = getRight top front
        print $ xs !! right

