import Data.List

pattern1 :: [Int] -> [String]
pattern1 x = map show [(x !! 0),(x !! 1),(x !! 5),(x !! 4),(x !! 0)]

pattern2 :: [Int] -> [String]
pattern2 x = map show [(x !! 1),(x !! 2),(x !! 4),(x !! 3),(x !! 1)]

pattern3 :: [Int] -> [String]
pattern3 x = map show [(x !! 2),(x !! 0),(x !! 3),(x !! 5),(x !! 2)]

solve :: [Int] -> [String] -> [Int]
solve list [] = []
solve list (x : xs)
  | isInfixOf x (unwords $ pattern1 list) = list !! 2 : solve list xs
  | isInfixOf x (unwords $ pattern2 list) = list !! 0 : solve list xs
  | isInfixOf x (unwords $ pattern3 list) = list !! 1 : solve list xs
  | isInfixOf x (unwords $ reverse $ pattern1 list) = list !! 3 : solve list xs
  | isInfixOf x (unwords $ reverse $ pattern2 list) = list !! 5 : solve list xs
  | isInfixOf x (unwords $ reverse $ pattern3 list) = list !! 4 : solve list xs

main = getContents >>= mapM_ print . (\(a:_:b) -> solve (map read $ words a) b) . lines