#include <bits/stdc++.h>
using namespace std;
struct treap {
  treap* c[2];
  long long key, val, sum, siz;
  treap(int v) : val(v), siz(1) {
    c[0] = c[1] = nullptr;
    key = rand();
  }
  ~treap() {
    delete c[0];
    delete c[1];
  }
  void recalc() {
    sum = (c[0] ? c[0]->sum : 0) + val + (c[1] ? c[1]->sum : 0);
    siz = (c[0] ? c[0]->siz : 0) + 1 + (c[1] ? c[1]->siz : 0);
  }
  treap* sift(treap*& p, int v) {
    if (not this) return this;
    bool d = v > val;
    if (v == val or not c[d]) return this;
    c[d]->sift(c[d], v);
    if (v < c[d]->val) return this;
    p = c[d];
    c[d] = p->c[!d];
    p->c[!d] = this;
    recalc(), p->recalc();
    return p;
  }
  void fall(treap*& p) {
    bool d = (!c[0] or c[1] and c[1]->key < c[0]->key);
    if (c[d] and c[d]->key < key) {
      sift(p, c[d]->val);
      fall(p->c[!d]);
    }
  }
  treap* before(treap*& p) {
    if (not this) return nullptr;
    treap* res = c[0];
    c[0] = nullptr, recalc();
    fall(p);
    return res;
  }
  treap* after(treap*& p) {
    if (not this) return nullptr;
    treap* res = c[1];
    c[1] = nullptr, recalc();
    fall(p);
    return res;
  }
  treap* cat(treap*& p, treap* w) {
    if (not this or not w) return this;
    sift(p, INT_MAX);
    p->c[1] = w;
    p->recalc();
    p->fall(p);
  }
  void insert(treap*& p, int v) {
    if (not this) {
      p = new treap(v);
      return;
    }
    if (v == val) return;
    c[v > val]->insert(c[v > val], v);
    if (c[v > val]->key < key) sift(p, v);
  }
  void erase(treap*& p, int v) {
    if (not this) return;
    if (v == val) {
      if (c[0] and c[1]) {
        treap* victim = c[1];
        while (victim->c[0]) victim = victim->c[0];
        key = victim->key;
        val = victim->val;
        c[1]->erase(c[1], val);
        fall(p);
      } else {
        p = c[!c[0]];
        c[0] = c[1] = 0;
        return delete this;
      }
    } else
      c[v > val]->erase(c[v > val], v);
    recalc();
  }
  void clear(treap*& p) {
    delete this;
    p = nullptr;
  }
  long long count() { return this ? sum : 0; }
  void debug(int dent = 0) {
    if (not this) return;
    c[0]->debug(dent + 1);
    for (int i = dent; i--;) printf(" ");
    printf("%d\n", val);
    c[1]->debug(dent + 1);
  }
  void linear_debug(bool p = true) {
    if (not this) {
      if (p) printf(" :::");
      return;
    }
    c[0]->linear_debug(false);
    printf(" %d", val);
    c[1]->linear_debug(false);
    if (p) printf(" :::");
  }
};
int main() {
  ios::sync_with_stdio(false);
  int ops;
  cin >> ops;
  treap* orbit[5] = {};
  for (int i = 5; i--;) orbit[i]->insert(orbit[i], 0);
  treap* tmp[5] = {};
  while (ops--) {
    string op;
    cin >> op;
    switch (op[0]) {
      case 'a': {
        int gnu;
        cin >> gnu;
        int l = 4;
        for (int i = 5; i--;)
          orbit[i]->sift(orbit[i], gnu),
              l = (orbit[i]->val <= orbit[l]->val ? i : l);
        for (int i = 5; i--;) tmp[i] = orbit[i]->after(orbit[i]);
        orbit[l]->insert(orbit[l], gnu);
        for (int i = 5; i--;) orbit[i]->cat(orbit[i], tmp[(i + 4) % 5]);
        break;
      }
      case 'd': {
        int gnu;
        cin >> gnu;
        int l = 4;
        for (int i = 5; i--;)
          orbit[i]->sift(orbit[i], gnu), l = (orbit[i]->val == gnu ? i : l);
        for (int i = 5; i--;) tmp[i] = orbit[i]->after(orbit[i]);
        for (int i = 5; i--;) orbit[i]->cat(orbit[i], tmp[(i + 1) % 5]);
        orbit[l]->erase(orbit[l], gnu);
        break;
      }
      default: {
        cout << (orbit[2]->count()) << endl;
        break;
      }
    }
  }
  for (int i = 5; i--;) delete orbit[i];
}
