import Control.Applicative
import Data.List
import Data.Maybe

table i =
  case i of
    0 -> [1, 3]
    1 -> [5, 2]
    2 -> [4, 0]
    3 -> [5, 1]
    4 -> [0, 3]
    5 -> [4, 2]

main :: IO ()
main = do
  dice <- words <$> getLine
  n <- read <$> getLine
  replicateM_ n $ do
    [top, front] <- map (fromJust . elemIndex) . words <$> getLine
    let sideIndex = 3 - (min top (5 - top)) - (min front (5 - front))
    putStrLn $ dice !! if elem front (table top) then sideIndex else 5 - sideIndex
    