// Problem: P5076 【深基16.例7】普通二叉树(简化版)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5076
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// Powered By cccycym 2024-03-26 17:41:58 中国标准时间
#include <bits/stdc++.h>
#define INF 2147483647
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
using namespace std;
char *p1,*p2,buf[100000];
int read()
{
int x=0,f=1;char ch=nc();
while(ch<48||ch>57){if(ch=='-')f=-1;ch=nc();}
while(ch>=48&&ch<=57){x=(x<<3)+(x<<1)+(ch^48);ch=nc();}
return x*f;
}
void write(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
int n, st;
vector<int> num;
int main()
{
n = read();
while(n -- )
{
int op, x;
op = read(), x = read();
if(op != 5 && st)
{
st = 0;
sort(num.begin(), num.end());
}
if(op == 1)
{
vector<int>::iterator p = lower_bound(num.begin(), num.end(), x);
write(p - num.begin() + 1);
puts("");
}
else if(op == 2)
{
write(num[x - 1]);
puts("");
}
else if(op == 3)
{
vector<int>::iterator p = lower_bound(num.begin(), num.end(), x);
if(p == num.begin()) write(-INF);
else write(*(p - 1));
puts("");
}
else if(op == 4)
{
vector<int>::iterator p = upper_bound(num.begin(), num.end(), x);
if(p == num.end()) write(INF);
else write(*p);
puts("");
}
else
{
num.push_back(x);
st = 1;
}
}
return 0;
}