main = do
    dice <- getLine
    let (n1:n2:n3:n4:n5:n6:_) = map (\x -> read x :: Int) $ words dice
    qcount <- getLine
    oqs <- getContents
    let qs = map (map (\x -> read x :: Int)) $ map words $ lines oqs
    let ans = map (solve (n1,n2,n3,n4,n5,n6)) qs
    mapM_ putStrLn $ map show ans

type Dice = (Int, Int, Int, Int, Int, Int)



sides :: Dice -> Int -> [Int]
sides (n1,n2,n3,n4,n5,n6) top
  | top == n1 = [n2,n3,n5,n4,n2]
  | top == n2 = [n1,n4,n6,n3,n1]
  | top == n3 = [n1,n2,n6,n5,n1]
  | top == n4 = [n1,n5,n6,n2,n1]
  | top == n5 = [n1,n3,n6,n4,n1]
  | top == n6 = [n2,n4,n5,n3,n2]
  | otherwise = sides (n1,n2,n3,n4,n5,n6) n1 -- NOT EXPECTED CASE

solve :: Dice -> [Int] -> Int
solve dice (top:front:_) = getRight dice top front

getRight :: Dice -> Int -> Int -> Int
getRight dice top front = next (sides dice top) front

next (x:xs) y
  | x == y && length xs > 0 = head xs
  | length xs == 0          = y         -- NOT EXPECTED CASE
  | otherwise               = next xs y

