#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
int n, m;
int seg[MAXN * 8];
set<int> ord;
set<int>::iterator it;
unordered_map<int, int> comp;
int discomp[MAXN];
long long sum;
queue<pair<char, int> > fila;
int bb(int pos, int ini, int fim, int val);
void update(int pos, int ini, int fim, int id, int val);
void defining(int pos, int ini, int fim, int p, int q);
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char type[5];
    int aux = 0;
    scanf(" %s", type);
    if (type[0] != 's') scanf("%d", &aux);
    if (aux != 0) ord.insert(aux);
    fila.push(make_pair(type[0], aux));
  }
  for (it = ord.begin(); it != ord.end(); it++) {
    comp[*it] = ++m;
    discomp[m] = *it;
  }
  for (int i = 0; i < n; i++) {
    char type = fila.front().first;
    int aux = comp[fila.front().second];
    fila.pop();
    if (type == 'a') update(1, 1, m, aux, 1);
    if (type == 'd') update(1, 1, m, aux, -1);
    if (type == 's') {
      sum = 0;
      defining(1, 1, m, 1, seg[1]);
      printf("%lld\n", sum);
    }
  }
}
void defining(int pos, int ini, int fim, int p, int q) {
  if (seg[pos] == 0) return;
  if (q - p + 1 < 5 && (p % 5 > 3 || q % 5 < 3)) return;
  if (ini == fim) {
    if (p % 5 == 3) sum += discomp[ini];
    return;
  }
  int mid = ini + ((fim - ini) >> 1), e = (pos << 1), d = e | 1;
  defining(e, ini, mid, p, p + seg[e] - 1);
  defining(d, mid + 1, fim, q - seg[d] + 1, q);
  return;
}
void update(int pos, int ini, int fim, int id, int val) {
  if (ini > id || fim < id) return;
  if (ini == fim) {
    seg[pos] += val;
    return;
  }
  int mid = ini + ((fim - ini) >> 1), e = (pos << 1), d = e | 1;
  update(e, ini, mid, id, val);
  update(d, mid + 1, fim, id, val);
  seg[pos] = seg[e] + seg[d];
  return;
}
