#include
#include
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
void Preorder(node *root)
{
queue<node *> q;
q.push(root);
while(!q.empty())
{
node *temp=q.front();
q.pop();
if(temp->left!=NULL)
{
cout<<temp->left->data<<" => ";
q.push(temp->left);
}
else
{
cout<<"END => ";
}
cout<<temp->data;
if(temp->right!=NULL)
{
cout<<" <= "<<temp->right->data;
q.push(temp->left);
}
else
{
cout<<" <= END";
}
cout<<endl;
}
}
node * Build_Tree(int *Pre,int *In,int s,int e)
{
static int i=0;
if(s>e)
return NULL;
node *root=new node(Pre[i]);
int index=-1;
for(int j=s;j<=e;j++)
{
if(In[j]==Pre[i])
{
index=j;
break;
}
}
i++;
root->left=Build_Tree(Pre,In,s,index-1);
root->right=Build_Tree(Pre,In,index+1,e);
return root;
}
int main()
{
int n,m;
cin>>n;
int *Pre=new int[n];
for(int i=0;i<n;i++)
cin>>Pre[i];
cin>>m;
int *In=new int[m];
for(int i=0;i<m;i++)
cin>>In[i];
node *root=Build_Tree(Pre,In,0,n-1);
Preorder(root);
}