import Control.Applicative

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

type Dice
    = [Int]

type Question
    = (Int, Int)

type Problem
    = (Dice, [Question])

type Result
    = (Dice, [Int])

table :: [[Int]]

table
    = [[0, 3, 5], [4, 0, 1], [2, 6, 0]]

right :: Question -> Int

right (top, front)
    | top < 4 && front < 4
        = table !! (top - 1) !! (front - 1)
        
    | top < 4
        = table !! (6 - front) !! (top - 1)
        
    | 3 < top && front < 4
        = table !! (front - 1) !! (6 - top)
        
    | otherwise
        = table !! (6 - top) !! (6 - front)

solve :: Problem -> Result

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

output :: Result -> String

output (dice, solutions)
    = intercalate "\n" [show $ dice !! (x - 1) | x <- solutions]
    
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