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

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

data Direction = E | N | S | W deriving (Eq, Show, Read)
data Relative = RIGHT | LEFT deriving (Eq, Show, Read)

main = do 
    [t, f, r, l, bk, bt] <- toIntList <$> getLine
    getLine
    nss <- map toIntList . lines <$> getContents
    let dice = Dice { top=t, front=f, right=r, left=l, back=bk, bottom=bt }
    mapM_ (\[x,y] -> print . right $ ans dice x y) nss


toIntList :: String -> [Integer]
toIntList = map read . words


rotate :: Dice -> Direction -> Dice
rotate Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} E =
    Dice{ right = t, top = l, left = bt, bottom = r, front = f, back = bk }

rotate Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} W =
    Dice{ left = t, top = r, right = bt, bottom = l, front = f, back = bk }

rotate Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} N =
    Dice{ back = t, top = f, front = bt, bottom = bk, right = r, left = l }

rotate Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} S =
    Dice{ front = t, top = bk, back = bt, bottom = f, right = r, left = l }


roll :: Dice -> Relative -> Dice
roll Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} RIGHT =
    Dice{ front = l, right = f, back = r, left = bk, top = t, bottom = bt }

roll Dice {top=t, right=r, left=l, front=f, back=bk, bottom=bt} LEFT =
    Dice{ front = r, left = f, back = l, right = bk, top = t, bottom = bt }


ans :: Dice -> Integer -> Integer -> Dice
ans d t f =
    let rolls = (scanl rotate d $ replicate 4 N) ++ (scanl roll d $ replicate 3 RIGHT)
        fd = fromJust $ find (\x -> front x == f) rolls in
        fromJust . find(\x -> top x == t) . scanl rotate fd $ replicate 4 E



