#include <bits/stdc++.h>
struct node {
  int fa, lt, rt, size, x;
} tree[110000];
int n, num, root;
char ch[10];
int build(int x, int p) {
  tree[++num].x = x;
  tree[num].lt = tree[num].rt = 0;
  tree[num].fa = p;
  tree[num].size = 1;
  return num;
}
inline void updata(int p) {
  tree[p].size = tree[tree[p].lt].size + tree[tree[p].rt].size + 1;
}
inline void zig(int p) {
  int q = tree[p].fa, r = tree[q].fa;
  tree[q].lt = tree[p].rt;
  if (tree[p].rt) tree[tree[p].rt].fa = q;
  tree[p].rt = q;
  tree[q].fa = p;
  tree[p].fa = r;
  if (r)
    if (tree[r].lt == q)
      tree[r].lt = p;
    else
      tree[r].rt = p;
  updata(q);
  updata(p);
}
inline void zag(int p) {
  int q = tree[p].fa, r = tree[q].fa;
  tree[q].rt = tree[p].lt;
  if (tree[p].lt) tree[tree[p].lt].fa = q;
  tree[p].lt = q;
  tree[q].fa = p;
  tree[p].fa = r;
  if (r)
    if (tree[r].lt == q)
      tree[r].lt = p;
    else
      tree[r].rt = p;
  updata(q);
  updata(p);
}
inline bool getlr(int p) { return tree[tree[p].fa].lt == p; }
inline void rotate1(int p) {
  if (getlr(p))
    zig(p);
  else
    zag(p);
}
void splay(int p, int tar) {
  while (tree[p].fa != tar && tree[tree[p].fa].fa != tar) {
    if (getlr(tree[p].fa) == getlr(p)) {
      rotate1(tree[p].fa);
      rotate1(p);
    } else {
      rotate1(p);
      rotate1(p);
    }
  }
  if (tree[p].fa != tar) rotate1(p);
  if (!tar) root = p;
}
int find(int p, int x) {
  if (tree[p].lt && tree[p].x > x) return find(tree[p].lt, x);
  if (tree[p].rt && tree[p].x < x) return find(tree[p].rt, x);
  return p;
}
int findq(int p) {
  if (!tree[p].rt)
    return p;
  else
    p = tree[p].rt;
  while (tree[p].lt) p = tree[p].lt;
  return p;
}
void insert1(int x) {
  if (!root) {
    root = build(x, 0);
    return;
  }
  int p = find(root, x);
  if (tree[p].x > x) {
    tree[p].lt = build(x, p);
    updata(p);
    return;
  } else {
    splay(p, 0);
    int q = findq(p);
    if (p == q)
      tree[p].rt = build(x, q);
    else
      tree[q].lt = build(x, q);
    updata(q);
    splay(q, 0);
  }
}
void delete1(int root, int x) {
  int p = find(root, x);
  splay(p, 0);
  int q = findq(p);
  int r = tree[q].fa;
  int q2 = findq(q);
  if (tree[q2].x < tree[r].x) r = q2;
  splay(r, 0);
  splay(p, r);
  tree[p].rt = 0;
  updata(p);
  updata(r);
}
int check(int p, int k) {
  int size = tree[p].size - tree[tree[p].rt].size;
  if (size < k) return check(tree[p].rt, k - size);
  if (k == size) return tree[p].x;
  if (k < size) return check(tree[p].lt, k);
}
void print(int p) {
  if (tree[p].lt) print(tree[p].lt);
  printf("p:%d lt:%d rt:%d size:%d x：%d \n", p, tree[p].lt, tree[p].rt,
         tree[p].size, tree[p].x);
  if (tree[p].rt) print(tree[p].rt);
}
int main() {
  scanf("%d", &n);
  root = 0;
  num = 0;
  memset(tree, 0, sizeof tree);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", &ch);
    int x;
    if (ch[0] == 'a') {
      scanf("%d", &x);
      insert1(x);
      continue;
    }
    if (ch[0] == 'd') {
      scanf("%d", &x);
      delete1(root, x);
      continue;
    }
    if (ch[0] == 's') {
      int ans = 0;
      for (int i = 3; i <= tree[root].size; i += 5) ans += check(root, i);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
