module Main where
import Control.Monad

ltable :: [(Int, [Int])]
ltable = [(1, cycle [2,3,5,4]), (2, cycle [6,2,1,4]), (3, cycle [6,5,1,2]),
          (4, cycle [6,2,1,5]), (5, cycle [6,4,1,3]), (6, cycle [5,3,2,4])]

question :: [Int] -> String -> Int
question [r1,r2,r3,r4,r5,r6] input =
  let [nu, nf] = fmap read (words input) :: [Int] in
      maybe 0 ((flip (!!) 1) . (dropWhile (/= nf))) (lookup nu ltable)

main :: IO ()
main = do
  dice <- map read . words <$> getLine :: IO [Int]
  q <- read <$> getLine
  replicateM_ q $ putStrLn . show =<< ((question dice) <$> getLine)