#include <bits/stdc++.h>
using namespace std;
long long son[200005][3], num[200005][3], f[200005], a[200005];
long long opt, n, i, root, x, node, ans, flag;
inline void Rotate(long long x, long long w) {
  long long y = f[x], z = f[y];
  son[y][3 - w] = son[x][w];
  if (son[x][w]) f[son[x][w]] = y;
  f[x] = z;
  num[y][3 - w] = num[x][w];
  num[x][w] += num[y][w] + 1;
  if (z) {
    if (y == son[z][1])
      son[z][1] = x;
    else
      son[z][2] = x;
  }
  f[y] = x;
  son[x][w] = y;
}
inline void splay(long long x) {
  long long y;
  while (f[x]) {
    y = f[x];
    if (!f[y]) {
      if (x == son[y][1])
        Rotate(x, 2);
      else
        Rotate(x, 1);
      continue;
    }
    if (y == son[f[y]][1]) {
      if (x == son[y][1])
        Rotate(y, 2), Rotate(x, 2);
      else
        Rotate(x, 1), Rotate(x, 2);
    } else {
      if (x == son[y][2])
        Rotate(y, 1), Rotate(x, 1);
      else
        Rotate(x, 2), Rotate(x, 1);
    }
  }
  root = x;
}
inline void insert(long long x, long long add) {
  if (add <= a[x]) {
    if (!son[x][1])
      son[x][1] = ++node, a[node] = add, f[node] = x;
    else
      insert(son[x][1], add);
    num[x][1]++;
  } else {
    if (!son[x][2])
      son[x][2] = ++node, a[node] = add, f[node] = x;
    else
      insert(son[x][2], add);
    num[x][2]++;
  }
}
inline void del(long long x, long long add) {
  if (!x) return;
  if (add == a[x]) {
    splay(x);
    if (!son[x][1] && !son[x][2]) {
      root = 0;
      return;
    }
    if (!son[x][1]) {
      root = son[x][2];
      f[son[x][2]] = 0;
      return;
    }
    if (!son[x][2]) {
      root = son[x][1];
      f[son[x][1]] = 0;
      return;
    }
    long long find = son[x][1], temp = son[x][2];
    while (son[find][2]) find = son[find][2];
    splay(find);
    son[find][2] = temp;
    f[temp] = find;
    return;
  }
  if (add < a[x])
    del(son[x][1], add);
  else
    del(son[x][2], add);
}
inline long long Kth(long long x, long long add, long long now) {
  insert(root, add);
  splay(node);
  long long ans = num[node][1] + 1;
  del(node, add);
  return ans;
}
inline long long Ask(long long x, long long k) {
  if (k == num[x][1] + 1) return a[x];
  if (k <= num[x][1]) return Ask(son[x][1], k);
  return Ask(son[x][2], k - num[x][1] - 1);
}
inline long long pred(long long x, long long k) {
  long long find = son[x][1];
  if (!find) return 0;
  while (son[find][2]) find = son[find][2];
  if (a[find] != k) flag = 1;
  return find;
}
inline long long succ(long long x, long long k) {
  long long find = son[x][2];
  if (!find) return 0;
  while (son[find][1]) find = son[find][1];
  if (a[find] != k) flag = 1;
  return find;
}
char s[200005];
int main() {
  long long cnt = 0;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    if (s[1] == 's') {
      long long ans = 0;
      for (long long j = 3; j < cnt; j += 5) {
        ans += Ask(root, j);
      }
      printf("%lld\n", ans);
    } else {
      scanf("%lld", &x);
      if (s[1] == 'd') {
        del(root, x);
        cnt--;
      } else {
        insert(root, x), splay(node);
        ;
        cnt++;
      }
    }
  }
}
