import Control.Applicative
import Control.Monad

data Dice = Dice {td::(Int,Int),ns::(Int,Int),ew::(Int,Int)} deriving Show
main = do
  d <- (fromList . map read . words) <$> getLine
  n <- read <$> getLine    
  v <- replicateM n ((map read . words) <$> getLine)
  mapM_ (putStrLn . show . solve d) v
  
solve :: Dice -> [Int] -> Int
solve d [t,s] 
  | snd(ns d)  ==s  && (fst (td d))==t  = fst(ew(d))
  | snd(ns d)  ==s = solve (opE d) [t,s] 
  | fst(ew d)  ==s = solve (opS(opW d)) [t,s] 
  | snd(ew d)  ==s = solve (opS(opE d)) [t,s] 
  | otherwise      = solve (opS d) [t,s]
  
fromList :: [Int] -> Dice
fromList [x1,x2,x3,x4,x5,x6] = Dice {td=(x1,x6),ns=(x5,x2),ew=(x3,x4)}
  
trans :: [Char] -> Dice -> Dice
trans [] x = x
trans (x:xs) d = case x of 
  'E' -> trans xs (opE d)
  'W' -> trans xs (opW d)
  'N' -> trans xs (opN d)
  'S' -> trans xs (opS d)

chg :: (Int,Int) -> (Int,Int)
chg (a,b) = (b,a)

opE :: Dice -> Dice
opE d = Dice (chg(ew d)) (ns d) (td d)
opW :: Dice -> Dice
opW d = Dice (ew d) (ns d) (chg(td d))

opN :: Dice -> Dice
opN d = Dice (chg(ns d)) (td d) (ew d)

opS :: Dice -> Dice
opS d = Dice (ns d) (chg(td d)) (ew d)