#include <iostream>
using namespace std;
struct TreeNode{
int data;
TreeNode * leftchild;
TreeNode * rightchild;
};
void insertBTS(TreeNode *&root, int data){
TreeNode *pnode = new TreeNode;
pnode->data = data;
pnode->leftchild = NULL;
pnode->rightchild = NULL;
if(root == NULL){
root = pnode;
printf("-1\n");
}
else{
TreeNode *pPre = root;
// TreeNode *Pcur;
while(true){
if(data > pPre->data && pPre->rightchild ==NULL){
// Pcur = root->rightchild;
pPre->rightchild = pnode;
printf("%d\n",pPre->data);
break;
}
else if(data < pPre->data && pPre->leftchild ==NULL){
pPre->leftchild = pnode;
printf("%d\n",pPre->data);
break;
}
else if(data < pPre->data){
pPre = pPre->leftchild;
}
else if(data > pPre->data){
pPre = pPre->rightchild;
}
}
}
}
int main() {
TreeNode *ans = NULL;
int n;
while (scanf("%d",&n)!=EOF) { // 注意 while 处理多个 case
for(int i=0;i<n;++i){
int x;
scanf("%d",&x);
insertBTS(ans,x);
}
}
}
// 64 位输出请用 printf("%lld")