using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20170410_AOJ_ALDS1_7D
{
    class ALDS1_7D
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int n = int.Parse(line);

                TreeNode[] arr = new TreeNode[n + 1];
                TreeNode parent = null;
                int[] ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

                for (int i = 0; i < n; i++)
                {
                    TreeNode tn = new TreeNode();
                    tn.Id = ns[i];
                    arr[tn.Id] = tn;
                    if (parent != null)
                    {
                        parent.Left = tn;
                        tn.Parent = parent;
                    }
                    parent = tn;
                }

                ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
                TreeNode restRoot = arr[1].GetRoot();
                for (int i = 0; i < n; i++)
                {
                    if(restRoot==null)
                    {
                        break;
                    }

                    TreeNode tn = arr[ns[i]];
                    if (restRoot == tn)
                    {
                        restRoot = tn.Left;
                        tn.Left = null;
                        tn.Right = restRoot;
                    }
                    else if (restRoot.Contains(tn))
                    {
                        restRoot = tn.Left;
                        if (restRoot != null)
                        {
                            tn.Left = null;
                            tn.Right = restRoot;
                        }
                    }
                    else
                    {
                        if(restRoot.Parent!=null)
                        {
                            restRoot.Parent.Right = null;
                        }
                        restRoot.Parent = tn;
                        tn.Right = restRoot;
                    }
                }

                TreeNode root = arr[1].GetRoot();
                WriteOrder(root);
            }
        }

        static void WriteOrder(TreeNode tn)
        {
            List<TreeNode> list = new List<TreeNode>();
            PostOrder(list, tn);
            Console.WriteLine(string.Join(" ", list.Select(x => x.Id.ToString()).ToArray()));
        }

        static void PostOrder(List<TreeNode> list, TreeNode tn)
        {
            if (tn.Left != null)
            {
                PostOrder(list, tn.Left);
            }

            if (tn.Right != null)
            {
                PostOrder(list, tn.Right);
            }

            list.Add(tn);
        }
    }

    class TreeNode
    {
        public int Id { get; set; }
        public TreeNode Parent { get; set; }
        public TreeNode Left { get; set; }
        public TreeNode Right { get; set; }
        public int Depth { get; internal set; }
        public int Height { get; internal set; }
        public TreeNode()
        {
        }

        public TreeNode GetRoot()
        {
            TreeNode temp = this;
            while (temp.Parent != null)
            {
                temp = temp.Parent;
            }

            return temp;
        }

        public bool Contains(TreeNode tn)
        {
            bool rv = false;
            rv = (
                this == tn ||
                (Left != null && Left.Contains(tn)) ||
                (Right != null && Right.Contains(tn)));
            return rv;
        }
    }
}


Measured memory_bytes: 9656