import Control.Monad
import Data.List

find_2 :: [Int] -> Int -> Int -> Int
find_2 arr a b = sides !! mod (get_value (findIndex (==b) sides) + 1) 4
  where sides = side_view arr a
    
-- get_value :: Maybe Int -> Int
get_value (Just x) = x
get_value Nothing = 0

side_view :: [Int] -> Int -> [Int]
side_view (a:b:c:d:e:f) x 
  | a == x = [b, c, e, d]
  | b == x = [a, d, f!!0, c]
  | c == x = [a, b, f!!0, e]
  | d == x = [a, e, f!!0, b]
  | e == x = [a, c, f!!0, d]
  | otherwise = [b, d, e, c]


main = do
  line <- getLine :: IO String
  let arr = map read (words line) :: [Int]
  nio <- getLine
  let n = read nio :: Int
  dicestr <- replicateM n getLine
  let dice = map (map read . words) dicestr :: [[Int]]
  forM_ dice $ \x -> do
    print $ find_2 arr (x!!0) (x!!1)