#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10, SQ = 300, MAXx = MAXn / SQ;
unordered_map<int, int> mp;
pair<char, int> q[MAXn];
bool flag[MAXn];
int n, sz;
long long ans[MAXx][5], cnt[MAXx];
vector<int> v;
void query(int a, int b) {
  int cntq = 0;
  for (int i = 0; i < sz; i += SQ) {
    int en = min(i + SQ, sz);
    if (a >= i && a < en) {
      int hp = cntq % 3;
      for (int j = 0; j < 5; j++) ans[i / SQ][j] = 0;
      for (int j = i; j < en; j++)
        if (flag[j]) ans[i / SQ][(cntq++) % 5] += v[j];
    } else if (a < i) {
      int hp[5];
      for (int j = 0; j < 5; j++) hp[j] = ans[i / SQ][(j + b + 5) % 5];
      for (int j = 0; j < 5; j++) ans[i / SQ][j] = hp[j];
    } else
      cntq += cnt[i / SQ];
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "sum") {
      q[i] = pair<char, int>('s', -1);
    } else if (s == "add") {
      int a;
      cin >> a;
      v.push_back(a);
      q[i] = pair<char, int>('+', a);
    } else {
      int a;
      cin >> a;
      q[i] = pair<char, int>('-', a);
    }
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  sz = v.size();
  for (int i = 0; i < sz; i++) {
    mp[v[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (q[i].first == 's') {
      long long ansq = 0;
      for (int j = 0; j < sz; j += SQ) ansq += ans[j / SQ][2];
      cout << ansq << '\n';
    } else if (q[i].first == '+') {
      int place = mp[q[i].second];
      flag[place] = true;
      cnt[place / SQ]++;
      query(place, -1);
    } else {
      int place = mp[q[i].second];
      flag[place] = false;
      cnt[place / SQ]--;
      query(place, 1);
    }
  }
}
