#include <bits/stdc++.h>
using namespace std;
int T;
vector<int> v[1000003], tmp;
long long cnt[1000003];
set<int> S;
void add() {
  int x;
  cin >> x;
  int group = x / 1000;
  v[group].push_back(x);
  for (int i = 0; i < v[group].size(); i++)
    if (v[group][i] > v[group].back())
      swap(v[group][i], v[group][v[group].size() - 1]);
  cnt[group] = 0;
  for (int i = 2; i < v[group].size(); i += 5) cnt[group] += v[group][i];
  if (v[group].size() >= 3) S.insert(group);
}
void del() {
  int x;
  cin >> x;
  int group = x / 1000;
  v[group].push_back(x);
  tmp.clear();
  for (int i = 0; i < v[group].size(); i++)
    if (v[group][i] != x) tmp.push_back(v[group][i]);
  v[group] = tmp;
  cnt[group] = 0;
  for (int i = 2; i < v[group].size(); i += 5) cnt[group] += v[group][i];
  if (v[group].size() < 3) S.erase(group);
}
void sum() {
  long long ans = 0;
  for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
    int tmp = cnt[*it];
    ans += tmp;
  }
  cout << ans << endl;
}
int main() {
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    if (s == "add") add();
    if (s == "del") del();
    if (s == "sum") sum();
  }
}
