import Control.Applicative
import Control.Monad

import Data.List
import Data.Maybe

table :: Int -> [Int]
table i =
  case i of
    0 -> [1, 3]
    1 -> [5, 2]
    2 -> [4, 0]
    3 -> [5, 1]
    4 -> [0, 3]
    5 -> [4, 2]

main :: IO ()
main = do
  dice <- words <$> getLine
  n <- read <$> getLine
  replicateM_ n $ do
    [top, front] <- map (fromJust . (`elemIndex` dice)) . words <$> getLine
    let sideIndex = 3 - min top (5 - top) - min front (5 - front)
    putStrLn $ dice !! if front `elem` table top then sideIndex else 5 - sideIndex