#include<iostream>
using namespace std;
struct node{
node* pre = NULL;
node* post = NULL;
int data;
int rank;
};
node* root = NULL;
void one(int num)
{
node* start = root;
while(start->data != num)
{
start = start->post;
}
cout<<start->rank<<endl;
}
void two(int num)
{
node* start = root;
while(start->rank != num)
{
start = start->post;
}
cout<<start->data<<endl;
}
void three(int num)
{
node* start = root;
while(start->data != num)
{
start = start->post;
}
if(start->pre == NULL)
cout<<"-2147483647"<<endl;
else
cout<<start->pre->data<<endl;
}
void four(int num)
{
node* start = root;
while(start->data != num)
{
start = start->post;
}
if(start->post == NULL)
cout<<"2147483647"<<endl;
else
cout<<start->post->data<<endl;
}
void five(int num)
{
node* cell = new node;
cell->data = num;
cell->post = NULL;
cell->pre = NULL;
if(root == NULL)
{
root = cell;
cell->rank = 1;
}
else
{
node* start = root;
node* temp = NULL;
while(start!=NULL && start->data < num)
{
temp = start;
start = start->post;
}
if(start == NULL)
{
temp->post = cell;
cell->pre = temp;
cell->rank = temp->rank + 1;
}
else if(start->data == num)
{
cell->post = start->post;
cell->pre = start->pre;
cell->rank = start->rank;
}
else if(start->data > num)
{
cell->pre = temp;
cell->post = start;
temp->post = cell;
start->pre = cell;
}
}
}
int main()
{
root = NULL;
int n,order,num;
cin>>n;
while(n--)
{
cin>>order>>num;
if(order == 1)
one(num);
else if(order == 2)
two(num);
else if(order == 3)
three(num);
else if(order == 4)
four(num);
else if(order == 5)
five(num);
}
return 0;
}