#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#define MAXN 10000
typedef struct node
{
int data;
struct node *lchild;
struct node *rchild;
}TreeNode, *Tree;
int inorder[MAXN], postorder[MAXN];
int count;
int line;
int min = INT_MAX;
int ans;
void build_tree(int n, int inorder[], int postorder[], Tree *root);
void find_least_node(int val, TreeNode *root);
void remove_tree(TreeNode *root);
int main()
{
int num;
char ch;
TreeNode *root = NULL;
#ifndef ONLINE_JUDGE
freopen("/home/wl/uva_in.txt", "r", stdin);
#endif
while (scanf("%d%c", &num, &ch) == 2)
{
if (line == 0)
{
inorder[count++] = num;
}
else if (line == 1)
postorder[count++] = num;
if (ch == '/n')
{
line++;
if (line == 2)
{
root = NULL;
build_tree(count, inorder, postorder, &root);
find_least_node(0, root);
remove_tree(root);
printf("%d/n", ans);
min = INT_MAX;
count = 0;
line = 0;
}
count = 0;
}
}
return 0;
}
void build_tree(int n, int inorder[], int postorder[], Tree *root)
{
int i;
int num;
if (n <= 0)
return;
*root = (Tree)malloc(sizeof(TreeNode));
(*root)->data = postorder[n - 1];
(*root)->lchild = NULL;
(*root)->rchild = NULL;
num = postorder[n - 1];
for (i = 0; i < n; i++)
if (inorder[i] == num)
break;
build_tree(i, inorder, postorder, &((*root)->lchild));
build_tree(n - i - 1, inorder + i + 1, postorder + i, &((*root)->rchild));
}
void find_least_node(int val, TreeNode *root)
{
if (root)
{
val += root->data;
if (!(root->lchild) && !(root->rchild))
{
if (val < min)
{
min = val;
ans = root->data;
}
}
else
{
if (root->lchild)
find_least_node(val, root->lchild);
if (root->rchild)
find_least_node(val, root->rchild);
}
}
}
void remove_tree(TreeNode *root)
{
if (root)
{
if (root->lchild)
remove_tree(root->lchild);
if (root->rchild)
remove_tree(root->rchild);
free(root);
}
}