LHeap<ll>h;
h.malloc(100,1);
map<string,ll>m;
string a[100];
ll@n,@q,b=0;
rep(q){
	ll@t;
	if(t==1){
		string@s;
		ll@r;
		ll d=b++;
if(b>100)exit(3);
		h.change(d,r);
		m[s]=d;
		a[d]=s;
	}
	if(t==2){
		ll@x;
		n-=x;
if(n<1)exit(4);
	}
	if(t==3){
		string@s;
		ll@x;
		ll d=m[s];
		ll r=h.val[d];
		if(r<4001){
			h.change(d,r+4001);
		}
		n+=x;
	}
	while(h.size>n){
		ll d=h.pop();
		wt(a[d]);
	}
}