#include <bits/stdc++.h>
int input[100010], param[100010];
int n;
int a[100010], m;
int q, r;
char s[64];
int bucket[1000][400];
int bucketn[1000];
long long medianprecalc[1000][5];
int nobuckets;
int bucketstart[1000];
void boette() {
  int i, j;
  puts("---PRINTOUT---");
  for (i = 0; i < n; i++)
    if (bucketn[i]) {
      printf("bucket %d:", i);
      for (j = 0; j < bucketn[i]; j++) printf(" %d", bucket[i][j]);
      printf(" [");
      for (j = 0; j < 5; j++) printf("%I64d ", medianprecalc[i][j]);
      printf("]\n");
    }
}
int findbucket(int key) {
  int left = 0, right = m, mid;
  while (left < right - 1) {
    mid = (left + right) / 2;
    if (key == bucketstart[mid])
      return mid;
    else if (key < bucketstart[mid])
      right = mid;
    else if (key > bucketstart[mid])
      left = mid + 1;
  }
  if (key < bucketstart[left]) return left - 1;
  return left;
}
int compi(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
long long ans;
void addnum(int key) {
  int bin = findbucket(key);
  medianprecalc[bin][0] = medianprecalc[bin][1] = medianprecalc[bin][2] =
      medianprecalc[bin][3] = medianprecalc[bin][4] = -1;
  bucket[bin][bucketn[bin]++] = key;
}
void delnum(int key) {
  int bin = findbucket(key), i;
  medianprecalc[bin][0] = medianprecalc[bin][1] = medianprecalc[bin][2] =
      medianprecalc[bin][3] = medianprecalc[bin][4] = -1;
  for (i = 0; i < bucketn[bin]; i++)
    if (bucket[bin][i] == key) {
      bucket[bin][i] = bucket[bin][--bucketn[bin]];
      return;
    }
  printf("ERROR");
}
void findsum() {
  int total = 0, i, j, k;
  ans = 0;
  for (i = 0; i < nobuckets; i++)
    if (medianprecalc[i][0] < 0) {
      qsort(bucket[i], bucketn[i], sizeof(int), compi);
      medianprecalc[i][0] = medianprecalc[i][1] = medianprecalc[i][2] =
          medianprecalc[i][3] = medianprecalc[i][4] = 0;
      k = 2;
      for (j = 0; j < bucketn[i]; j++) {
        medianprecalc[i][k] += bucket[i][j];
        k--;
        if (k < 0) k = 4;
      }
    }
  for (i = 0; i < nobuckets; i++) {
    ans += medianprecalc[i][total];
    total += bucketn[i];
    total %= 5;
  }
  printf("%I64d\n", ans);
}
void solve() {
  int i;
  for (i = 0; i < nobuckets; i++)
    medianprecalc[i][0] = medianprecalc[i][1] = medianprecalc[i][2] =
        medianprecalc[i][3] = medianprecalc[i][4] = -1;
  for (i = 0; i < n; i++) {
    if (!input[i])
      addnum(param[i]);
    else if (input[i] == 1)
      delnum(param[i]);
    else
      findsum();
  }
}
int main() {
  int last = 0, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    if (!strcmp(s, "sum"))
      input[i] = 2, last = i + 1;
    else if (!strcmp(s, "add"))
      input[i] = 0, scanf("%d", &param[i]);
    else
      input[i] = 1, scanf("%d", &param[i]);
  }
  for (i = m = 0; i < n; i++)
    if (!input[i]) a[m++] = param[i];
  qsort(a, m, sizeof(int), compi);
  q = (sqrt(m) + 1) * 2;
  r = n / q;
  nobuckets = q;
  if (!r) r = 1;
  for (i = j = 0; i < nobuckets; j += r, i++) bucketstart[i] = a[j];
  bucketstart[i] = 2000000000;
  for (i = 0; i < nobuckets; i++) bucketn[i] = 0;
  solve();
  return 0;
}
