import Control.Applicative

import Control.Monad
    (replicateM)
    
import Data.List
    (intercalate)
    
import Data.Map.Strict
    ((!), Map, fromList)

type Dice
    = [Int]

type Question
    = (Int, Int)
    
type Table
    = Map Int (Map Int Int)

type Problem
    = (Dice, [Question])

type Result
    = [Int]

table :: Dice -> Table

table [s1, s2, s3, s4, s5, s6]
    = fromList [(s1, l1), (s2, l2), (s3, l3), (s4, l4), (s5, l5), (s6, l6)] where
        l1 = fromList [(s1, 0), (s2, s3), (s3, s5), (s4, s2), (s5, s4), (s6, 0)]
        l2 = fromList [(s1, s4), (s2, 0), (s3, s1), (s4, s6), (s5, 0), (s6, s3)]
        l3 = fromList [(s1, s2), (s2, s6), (s3, 0), (s4, 0), (s5, s1), (s6, s5)]
        l4 = fromList [(s1, s5), (s2, s1), (s3, 0), (s4, 0), (s5, s6), (s6, s2)]
        l5 = fromList [(s1, s3), (s2, 0), (s3, s6), (s4, s1), (s5, 0), (s6, s4)]
        l6 = fromList [(s1, 0), (s2, s4), (s3, s2), (s4, s5), (s5, s3), (s6, 0)]

right :: Dice -> Question -> Int

right dice (top, front)
    = table dice ! top ! front

solve :: Problem -> Result

solve (dice, questions)
    = map (right dice) questions

output :: Result -> String

output result
    = intercalate "\n" $ map show result
    
input :: IO Problem

input
    = do
        dice <- map read <$> words <$> getLine
        n <- read <$> getLine
        questions <- replicateM n inputQuestion
        return (dice, questions)
        
inputQuestion :: IO Question

inputQuestion
    = do
        [top, front] <- map read <$> words <$> getLine
        return (top, front)

main :: IO ()

main
    = putStrLn <$> output =<< solve <$> input