#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>

using namespace std;

#define print(x) cout<<x<<endl
#define input(x) cin>>x
#define SIZE 100100

typedef long long llint;

inline int lowbit(int x){return (x)&(-x);}

struct BIT
{
	int baum[SIZE];
	inline void init(){memset(baum,0,sizeof(baum));}
	
	inline void add(int pos,int val)
	{
		while(pos<SIZE)
		{
			baum[pos]+=val;
			pos+=lowbit(pos);
		}
	}
	inline int sum(int pos)
	{
		int res=0;
		while(pos>0)
		{
			res+=baum[pos];
			pos-=lowbit(pos);
		}
		return res;
	}
	inline int sum(int a,int b)
	{
		return sum(b)-sum(a-1);
	}
};

struct command
{
	int type,val;
	command(){}
	command(int itype,int ival)
	{
		type=itype;val=ival;
	}
};

int array[SIZE];
command op[SIZE];

int n;
int sz;

int main()
{
	BIT bit;
	char cmd[12];
	int a;
	map<int,int> mp;
	set<int> st;
	while(input(n))
	{
		bit.init();
		sz=1;
		st.clear();
		mp.clear();
		for(int i=0;i<n;i++)
		{
			scanf("%s",cmd);
			if(*cmd!='s')
			{
				scanf("%d",&a);
				if(st.find(a)==st.end())
				{
					array[sz++]=a;
					st.insert(a);
				}
				if(*cmd=='a')
				{
					op[i]=command(0,a);
				}
				else if(*cmd=='d')
				{
					op[i]=command(1,a);
				}
			}
			else
			{
				op[i]=command(2,-1);
			}
		}
		sort(array+1,array+sz);
		for(int i=1;i<sz;i++)
		{
			mp[array[i]]=i;
		}
		
		for(int i=0;i<n;i++)
		{
			if(op[i].type==0)
			{
				bit.add(mp[op[i].val],1);
				//print(mp[op[i].val]);
			}
			else if(op[i].type==1)
			{
				bit.add(mp[op[i].val],-1);
				//print(mp[op[i].val]);
			}
			else
			{
				llint sum=0;
				int ptr=1;
				int val=3;
				while(ptr<sz)
				{
					int l=ptr,r=sz-1;
					while(l<=r)
					{
						int mid=(l+r)>>1;
						if(bit.sum(ptr,mid)>=val) r=mid-1;
						else l=mid+1;
					}
					if(l<sz) sum+=array[l];
					else break;
					ptr=l+1;
					val+=5;
				}
				printf("%I64d\n",sum);
			}
		}
	}
	return 0;
}



