import Control.Monad

data Dice = Dice {top :: Int,
                  south :: Int,
                  east :: Int,
                  west :: Int,
                  north :: Int,
                  bottom :: Int}
fromList :: [Int] -> Dice
fromList [t,s,e,w,n,b] = Dice t s e w n b

rotate :: Dice -> Char -> Dice
rotate d 'N' = d {bottom = north d,
                  north = top d,
                  top = south d,
                  south = bottom d}
rotate d 'S' = d {bottom = south d,
                  south = top d,
                  top = north d,
                  north = bottom d}
rotate d 'E' = d {bottom = east d,
                  east = top d,
                  top = west d,
                  west = bottom d}
rotate d 'W' = d {bottom = west d,
                  west = top d,
                  top = east d,
                  east = bottom d}
rotate d 'c' = d {north = west d,
                  west = south d,
                  south = east d,
                  east = north d}
rotate d 'C' = rotateAll d "ccc"

rotateAll :: Dice -> [Char] -> Dice
rotateAll d [] = d
rotateAll d (op:ops) = rotateAll (rotate d op) ops

rotateWithTop :: Dice -> Int -> Dice
rotateWithTop d t | t == top d = d
                  | t == north d = rotate d 'S'
                  | t == south d = rotate d 'N'
                  | t == east d = rotate d 'W'
                  | t == west d = rotate d 'E'
                  | t == bottom d = rotateAll d "NN"

rotateWithMatch :: Dice -> [Int] -> Dice
rotateWithMatch d [t, s] | t /= top d =
                           rotateWithMatch (rotateWithTop d t) [t, s]
                         | s == south d = d
                         | s /= south d =
                           rotateWithMatch (rotate d 'c') [t, s]

main :: IO ()
main = do
  d <- fmap (fromList . map read . words) getLine
  q <- readLn
  qs <- replicateM q $ fmap (map read . words) getLine
  mapM_ (putStrLn . show . east . rotateWithMatch d) qs