import Control.Applicative

import Control.Monad
    (replicateM)
    
import Data.List
    (elemIndex, intercalate)

type Dice
    = [Int]

type Question
    = (Int, Int)
    
type Table
    = [[Int]]

type Problem
    = (Dice, [Question])

type Result
    = [Int]

table :: Dice -> Table

table [s1, s2, s3, s4, s5, s6]
    = [
        [0, s3, s5, s2, s4, 0],
        [s4, 0, s1, s6, 0, s3],
        [s2, s6, 0, 0, s1, s5],
        [s5, s1, 0, 0, s6, s2],
        [s3, 0, s6, s1, 0, s4],
        [0, s4, s2, s5, s3, 0]
        ]

right :: Dice -> Question -> Int

right dice (top, front)
    = table dice !! ti !! fi where
        Just ti = elemIndex top dice
        Just fi = elemIndex front dice

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