#include<bits/stdc++.h>

using namespace std;

#define tm (tl + tr >> 1)
#define N 100005

map < int , int > H, M;

int lz[4*N],A[N],a[N],n,t,i,j,w,pos,ddd,l,r,x;

long long s[4*N][6];

char ss[N][3];

int qry(int v, int tl, int tr){
	if(tl > r || tr < l)
		return 0;
	if(tl >= l && tr <= r)
		return s[v][5];
	return qry(v*2, tl, tm) + qry(v*2+1, tm+1, tr);
}
void g(int v, int d){

	if(d < 0) {
		x = s[v][0];
		s[v][0] = s[v][1];
		s[v][1] = s[v][2];
		s[v][2] = s[v][3];
		s[v][3] = s[v][4];
		s[v][4] = x;
	}
	else{
		x = s[v][0];
		s[v][0] = s[v][4];
		s[v][4] = s[v][3];
		s[v][3] = s[v][2];
		s[v][2] = s[v][1];
		s[v][1] = x;
	}
}
void push(int v){

	lz[v] %= 5;
	for(j=0;j<abs(lz[v]);j++)
		g(v*2,lz[v]);

	for(j=0;j<abs(lz[v]);j++)
		g(v*2+1,lz[v]);

	lz[v*2] += lz[v];
	lz[v*2+1] += lz[v];
	lz[v] = 0;
}
void up2(int v, int tl, int tr){
	if(tl == tr)
	{ s[v][w] = ddd; return; }
	if(w < 5) push(v);
	if(pos <= tm)
		up2(v*2, tl, tm);
	else
		up2(v*2+1, tm+1, tr);
	for(j=0;j<=5;j++)
		s[v][j] = s[v*2][j] + s[v*2+1][j];
}
void up(int v, int tl, int tr){
	if(tl > r || tr < l)
		return;
	if(tl >= l && tr <= r){
		lz[v] += ddd;
		g(v,ddd);
		return;
	}
	push(v);
	up(v*2, tl, tm); up(v*2+1, tm+1, tr);
	for(j=0;j<5;j++)
		s[v][j] = s[v*2][j] + s[v*2+1][j];
}
int main(){

	cin >> n;

	for(i=1;i<=n;i++){
		scanf(" %s",ss[i]);
		if(ss[i][0] != 's')
			scanf("%d",&a[i]);
		if(a[i] && !H[ a[i] ])
			A[++t] = a[i];
		H[ a[i] ] = 1;
	}

	sort(A+1 , A+t+1);

	for(i=1;i<=t;i++)
		M[ A[i] ] = i;

	for(i=1;i<=n;i++){

		if(ss[i][0] != 's'){

			w = 5;  l = 1;
			pos = r = M[ a[i] ]; 
			ddd = (ss[i][0] == 'a' ? 1 : 0);
			up2(1,1,t);

			w = ( qry(1,1,t) + (ss[i][0] == 'd') ) % 5;
			ddd = (ss[i][0] == 'a' ? a[i] : 0);
			up2(1,1,t);

			ddd = (ss[i][0] == 'a' ? 1 : -1);
			l = M[ a[i] ] + 1;
			r = t;
			up(1,1,t);
		}

		else
			printf("%lld\n", s[1][3]);
	}

	return 0;
}
