{-
?????????: Tree - Reconstruction of a Tree
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_D
?????°:
19:39:05 koch_curve???Haskell?\?????????°??§?\??????????????????§?????????????????£???????????????
19:42:47 ??¨??????????????\???????????????????????????????????¨read???????????????
19:51:01 ????????§?????????????????????????????????
19:52:44 ?????????????????£???
-}

import Control.Applicative
import Control.Monad

main = do
 _ <- getLine
 arr1 <- fmap words getLine
 arr2 <- fmap words getLine
 putStrLn . unwords $ foo arr1 arr2

foo :: (Eq a) => [a] -> [a] -> [a]
foo [] [] = []
foo preorder@(a:l1r1) inorder = l3 ++ r3 ++ [a] where
 (l2, _:r2) = break (== a) inorder
 (l1,r1) = splitAt (length l2) l1r1
 l3 = foo l1 l2
 r3 = foo r1 r2