import scala.io.StdIn.{readInt, readLine}
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val pre = readLine.split(" ").map(_.toInt);
        val in = readLine.split(" ").map(_.toInt);
        val tree = Tree();
        tree.build(n, pre, in);
        println(tree.postorder().mkString(" "));
    }
}

case class Tree() {
    case class TreeNode(value: Int, left: TreeNode, right: TreeNode);
    var root: TreeNode = null;
    
    def isEmpty(): Boolean = root == null;
    
    def preorder(): List[Int] = {
        def _preorder(node: TreeNode, ret: List[Int]): List[Int] = {
            if (node == null) {
                ret;
            } else {
                val ret1 = node.value :: ret;
                val ret2 = _preorder(node.left, ret1);
                val ret3 = _preorder(node.right, ret2);
                ret3;
            }
        }
        _preorder(root, Nil).reverse;
    }
    
    def postorder(): List[Int] = {
        def _preorder(node: TreeNode, ret: List[Int]): List[Int] = {
            if (node == null) {
                ret;
            } else {
                val ret1 = _preorder(node.left, ret);
                val ret2 = _preorder(node.right, ret1);
                val ret3 = node.value :: ret2;
                ret3;
            }
        }
        _preorder(root, Nil).reverse;
    }
    
    def build(size: Int, pre: Array[Int], in: Array[Int]): Unit = {
        // pre の index を示すポインタ
        var prePtr = 0
        def buildSubTree(lip: Int, rip: Int): TreeNode = {
            if (lip > rip) {
                // 探索するノードが存在しない場合はnullを返す
                null;
            } else if (lip == rip) {
                //  探索するノードが1つしかない場合は、子ノードが null の部分木を返す
                val value: Int = pre(prePtr);
                TreeNode(value, null, null);
            } else {
                val value: Int = pre(prePtr);
                val pivot: Int = (lip to rip).find(i => in(i) == value).get; // 無いわけない
                prePtr += 1;
                val leftSubTree = buildSubTree(lip, pivot - 1);
                prePtr += 1;
                val rightSubTree = buildSubTree(pivot + 1,  rip);
                TreeNode(value, leftSubTree, rightSubTree);
            }
        }
        root = if (size < 1) null else {buildSubTree(0, size - 1)};
    }
}

