#include <bits/stdc++.h>
using namespace std;
int a[100005], t[100005], hs[100005];
vector<int> vec[20005];
int main() {
  int n;
  char buf[10];
  scanf("%d", &n);
  int addcnt = 0;
  for (int tt = 0; tt < n; tt++) {
    scanf("%s", buf);
    if (buf[0] == 'a') {
      scanf("%d", &a[tt]);
      hs[tt] = a[tt];
      t[tt] = 0;
      addcnt++;
    } else if (buf[0] == 'd') {
      scanf("%d", &a[tt]);
      t[tt] = 1;
      hs[tt] = a[tt];
    } else {
      t[tt] = 2;
    }
  }
  sort(hs, hs + n);
  int cnt = unique(hs, hs + n) - hs;
  for (int i = 0; i < n; i++) {
    if (t[i] == 0) {
      a[i] = lower_bound(hs, hs + cnt, a[i]) - hs;
      int index = a[i] / 5;
      vector<int>::iterator it =
          lower_bound(vec[index].begin(), vec[index].end(), a[i]);
      vec[index].insert(it, a[i]);
    } else if (t[i] == 1) {
      a[i] = lower_bound(hs, hs + cnt, a[i]) - hs;
      int index = a[i] / 5;
      vector<int>::iterator it =
          lower_bound(vec[index].begin(), vec[index].end(), a[i]);
      vec[index].erase(it);
    } else {
      long long ans = 0;
      for (int k = 2, c = 0, j = 0; k < addcnt; k += 5) {
        while (k - c > (int)vec[j].size() - 1 && j < cnt / 5 + 1) {
          c += vec[j].size();
          ++j;
        }
        if ((int)vec[j].size() > k - c) ans += vec[j][k - c];
      }
      printf("%I64d\n", ans);
    }
  }
  return 0;
}
