#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;


using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> rbtree;

int main()
{
	rbtree a;
	int n;cin>>n;
	while(n--)
	{
		int o,x;cin>>o>>x;
		if(o==1)
			a.insert(x);
		else if(o==2)
			a.erase(x);
		else if(o==3)
			cout<<a.order_of_key(x)+1<<"\n";
		else if(o==4)
			cout<<*a.find_by_order(x-1)<<"\n";
		else if(o==5)
			cout<<*prev(a.lower_bound(x))<<"\n";
		else if(o==6)
			cout<<*a.upper_bound(x)<<"\n";
	}
}