module Main where

import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
import Data.Array
import Data.Maybe
import qualified Data.Map as M
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad
import Text.Printf

data Tree = Nil | Node Int Tree Tree deriving (Eq, Show)

solve :: [Int] -> [Int] -> [Int]
solve pro ino = iter root where
    iter Nil = []
    iter (Node id left right) = iter left ++ iter right ++ [id]    

    root = iter pro ino where
        iter [] [] = Nil
        iter (p:pol) iol = Node p (iter lpro lino) (iter rpro rino) where
            (lino, ri) = break (==p) iol
            rino = drop 1 ri
            lpro = intersect pol lino
            rpro = intersect pol rino

            
readIntLn:: IO Int
readIntLn = (fst . fromJust . B.readInt) <$> B.getLine

readIntList:: IO [Int]
readIntList = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

print':: [Int] -> IO ()
print' [x] = putStrLn (show x)
print' (x:xs) = do
    putStr $ show x ++ " "
    print' xs

main :: IO() 
main = do
    _ <- readIntLn
    l <- readIntList
    m <- readIntList
    print' $ solve l m

