#include<cstdio>
#include<stdlib.h>
#define ll long long
const ll N=1e6+5;
ll C[N];
struct FHQ{
ll root,xs,ys,zs,idx;
struct Node{
ll ls,rs,val,key,size;
}tr[N];
inline void update(ll rt){tr[rt].size=tr[tr[rt].ls].size+tr[tr[rt].rs].size+1;return ;}
inline ll get_node(ll key){
tr[++idx].key=key;
tr[idx].val=rand();
tr[idx].size=1;
return idx;
}
inline void split(ll p,ll key,ll &x,ll &y){
if(!p){x=y=0;return ;}
else{
if(tr[p].key<=key){
x=p;
split(tr[p].rs,key,tr[p].rs,y);
}else{
y=p;
split(tr[p].ls,key,x,tr[p].ls);
}
update(p);
}
}
inline ll merge(ll x,ll y){
if(!x||!y)return x+y;
else{
if(tr[x].val<tr[y].val){
tr[x].rs=merge(tr[x].rs,y);
update(x);
return x;
}else{
tr[y].ls=merge(x,tr[y].ls);
update(y);
return y;
}
}
}
inline void insert(ll key){
split(root,key,xs,ys);
root=merge(merge(xs,get_node(key)),ys);
}
inline void del(ll key){
split(root,key,xs,zs);
split(xs,key-1,xs,ys);
ys=merge(tr[ys].ls,tr[ys].rs);
root=merge(merge(xs,ys),zs);
}
inline ll get_p(ll key){
split(root,key-1,xs,ys);
ll p=xs,knum;
while(tr[p].rs)p=tr[p].rs;
knum=tr[p].key;
root=merge(xs,ys);
return knum;
}
inline ll get_s(ll key){
split(root,key,xs,ys);
ll p=ys,knum;
while(tr[p].ls)p=tr[p].ls;
knum=tr[p].key;
root=merge(xs,ys);
return knum;
}
inline bool find(ll key){
split(root,key,xs,ys);
ll p=xs;
bool flag=0;
while(tr[p].ls)p=tr[p].ls;
if(tr[p].key==key)flag=1;
root=merge(xs,ys);
return flag;
}
inline ll sum1(ll p){
if(!p)return 0;
else return C[tr[p].key]+sum1(tr[p].ls)+sum1(tr[p].rs);
}
inline ll sum2(ll p){
if(!p)return 0;
else return ((tr[p].key==0||tr[p].key==1000001)?0:tr[p].key)+sum2(tr[p].ls)+sum2(tr[p].rs);
}
}fhq;
signed main(){
ll op;
fhq.insert(0);
fhq.insert(1000001);
while(scanf("%lld",&op)!=EOF){
if(op==1){
ll w,c;
scanf("%lld%lld",&w,&c);
if(fhq.find(c))
continue;
else{
fhq.insert(c);
C[c]=w;
}
}
else if(op==2){
ll keynum;
keynum=fhq.get_p(1000001);
if(keynum!=1000001&&keynum!=0){
C[keynum]=0;
fhq.del(keynum);
}else{
continue;
}
}else if(op==3){
ll keynum;
keynum=fhq.get_s(0);
if(keynum!=1000001&&keynum!=0){
C[keynum]=0;
fhq.del(keynum);
}else{
continue;
}
}else if(op==-1){
printf("%lld %lld\n",fhq.sum1(fhq.root),fhq.sum2(fhq.root));
return 0;
}
}
return 0;
}