import           Control.Applicative
import           Control.Monad
import           Data.List

type Dice=[Int]

rotate:: Dice -> Char -> Dice
rotate [u,f,r,l,b,d] 'E' = [l,f,u,d,b,r]
rotate [u,f,r,l,b,d] 'N' = [f,d,r,l,u,b]
rotate [u,f,r,l,b,d] 'S' = [b,u,r,l,d,f]
rotate [u,f,r,l,b,d] 'W' = [r,f,d,u,b,l]
rotate xs _              = xs

search :: [Dice]->Int->Int->Dice
search dices up front
  | null found =  search [ rotate d opt | d <-dices, opt<-"ENSW" ] up front
  | otherwise = head found
  where
    found = [dice | dice@[u,f,r,l,b,d]<-dices, u==up, f==front]

main :: IO ()
main = do
  dice <- map read . words <$> getLine
  qs <- getLine
  query <- replicateM (read qs) (map read . words <$> getLine)
  putStr $ unlines $ map show [search [dice] up front !!2 | [up, front]<-query]

