import Control.Monad
import Control.Applicative

question :: [Int] -> String -> Int
question [r1,r2,r3,r4,r5,r6] input =
  let [nu, nf] = fmap read (words input) :: [Int]
      ltable = [(r1, cycle [r2,r3,r5,r4]), (r2, cycle [r6,r3,r1,r4]), (r3, cycle [r6,r5,r1,r2]), (r4, cycle [r6,r2,r1,r5]), (r5, cycle [r6,r4,r1,r3]), (r6, cycle [r5,r3,r2,r4])]
   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)