#include<cstdio>
#include<string>
using namespace std;
struct TreeNode{
int data;
TreeNode * leftChild;
TreeNode * rightChild;
};
void insertBST(TreeNode * &root, int data){
TreeNode *pNewNode = new TreeNode;
pNewNode->data = data;
pNewNode->leftChild = NULL;
pNewNode->rightChild=NULL;
if(root == NULL){
root = pNewNode;
printf("-1\n");
}
else{
TreeNode *pPre = root;
TreeNode *pCur;
while (true){
if (data < pPre->data){
pCur = pPre->leftChild;
if(pCur == NULL){
pPre->leftChild = pNewNode;
printf("%d\n",pPre->data);
break;
}
else{
pPre = pCur;
}
}
else{
pCur = pPre->rightChild;
if (pCur == NULL){
pPre->rightChild = pNewNode;
printf("%d\n",pPre->data);
break;
}
else{
pPre = pCur;
}
}
}
}
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
TreeNode * root = NULL;
//int array[] = {2,3,5,1,4};
for(int i = 0; i < n; ++i){
int num;
scanf("%d",&num);
insertBST(root , num);
}
}
}