#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define ll long long
#define ull unsigned ll

#define ULL_HASH_SIZE 200000
ull ull_hash_n[ULL_HASH_SIZE]; int ull_hash_d[ULL_HASH_SIZE];

void ullHashClear(void){ memset(ull_hash_d,0,ULL_HASH_SIZE*sizeof(int)); }
int ullHashFunction(ull a){ return (a*1007)%ULL_HASH_SIZE; }

int ullHashGet(ull a){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  if(ull_hash_n[i]!=a) return 0; return ull_hash_d[i];
}

void ullHashSet(ull a,int n){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  ull_hash_n[i]=a; ull_hash_d[i]=n;
}

int ullHashIncrease(ull a){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  ull_hash_n[i]=a; return ++ull_hash_d[i];
}


#define G 250

void intSort(int d[],int s){int i=-1,j=s,k,t;if(s<=1)return;k=(d[0]+d[s-1])/2;for(;;){while(d[++i]<k);while(d[--j]>k);if(i>=j)break;t=d[i];d[i]=d[j];d[j]=t;}intSort(d,i);intSort(d+j+1,s-j-1);}

char op[110000]; int in[110000];
int use_num[110000], use_size;
int memory[500000];

int gn;
int *contain[410][5], sz[410][5]; ll sum[410][5];

int *con_mem[5], sz_mem[5]; ll sum_mem[5];
void rotate(int g, int shift){
  int i;

  rep(i,5){
    con_mem[i] = contain[g][i];
    sz_mem[i]  = sz[g][i];
    sum_mem[i] = sum[g][i];
  }
  rep(i,5){
    contain[g][i] = con_mem[(i+shift)%5];
    sz[g][i]      = sz_mem[(i+shift)%5];
    sum[g][i]     = sum_mem[(i+shift)%5];
  }
}

int arr[410];
void recalc(int g,int add,int del){
  int i, j, arr_sz=0;

  rep(i,5) rep(j,sz[g][i]){
    if(contain[g][i][j]==del) continue;
    arr[arr_sz++] = contain[g][i][j];
  }
  if(add>=0) arr[arr_sz++] = add;

  intSort(arr,arr_sz);
  rep(i,5) sz[g][i] = sum[g][i] = 0;
  rep(i,arr_sz){
    j = i%5;
    contain[g][j][sz[g][j]] = arr[i];
    sum[g][j] += use_num[arr[i]];
    sz[g][j]++;
  }
}

void op_add(int t){
  int i;
  int g = t/G;
  recalc(g,t,-1);
  REP(i,g+1,gn) rotate(i, 1);
}

void op_del(int t){
  int i;
  int g = t/G;
  recalc(g,-1,t);
  REP(i,g+1,gn) rotate(i, 4);
}

ll op_sum(void){
  int i, j, bef=0, id; ll res = 0;
  rep(i,gn){
    id = 2-bef; id%=5; if(id<0) id+=5;
    res += sum[i][id];
    rep(j,5) bef += sz[i][j];
  }
  return res;
}

int main(){
  int i,j,k,l,m,n;
  int add;
  ll res;

  add = 0;
  rep(i,410)rep(j,5) contain[i][j]=memory+add, add+=260;

  while(scanf("%d",&n)==1){
    rep(i,n){
      scanf("%s",op+i);
      if(op[i]!='s') scanf("%d",in+i);
    }

    use_size = 0;
    rep(i,n) if(op[i]=='a') use_num[use_size++] = in[i];

    intSort(use_num, use_size);
    ullHashClear();
    rep(i,use_size) ullHashSet(use_num[i], i+1);

    gn = (use_size+G-1) / G;

    rep(i,gn) rep(j,5) sum[i][j] = sz[i][j] = 0;

    rep(i,n){
      if(op[i]=='a'){
        k = ullHashGet(in[i])-1;
        op_add(k);
        continue;
      }
      if(op[i]=='d'){
        k = ullHashGet(in[i])-1;
        op_del(k);
        continue;
      }
      if(op[i]=='s'){
        res = op_sum();
        printf("%I64d\n",res);
        continue;
      }
    }
  }

  return 0;
}
