#include <bits/stdc++.h>
using namespace std;
const int B = 300;
const int LEN = 1e5 + 5;
const int o = (LEN + B - 1) / B + 5;
int n;
int a[100005], z[100005], tp[100005], cnt[o];
int w[o][B];
long long sum[o][10];
pair<int, int> y[100005];
long long calc() {
  int cur = 0;
  long long ret = 0;
  for (int i = 0; i < o; i++) {
    int p = (7 - cur) % 5;
    ret += sum[i][p];
    cur = (cur + cnt[i]) % 5;
  }
  return ret;
}
void add(int p, int x) {
  int b = p / B;
  for (int i = cnt[b]; i >= 0; i--) {
    if (i != 0 && w[b][i - 1] > x)
      w[b][i] = w[b][i - 1];
    else {
      w[b][i] = x;
      break;
    }
  }
  cnt[b]++;
  memset(sum, 0, sizeof(sum));
  for (int i = 0; i < cnt[b]; i++) {
    sum[b][i % 5] += w[b][i];
  }
  return;
}
void del(int p, int x) {
  int b = p / B;
  for (int i = 0; i < cnt[b]; i++) {
    if (w[b][i] == x) {
      for (int j = i; j < cnt[b] - 1; j++) {
        w[b][j] = w[b][j + 1];
      }
    }
  }
  cnt[b]--;
  memset(sum, 0, sizeof(sum));
  for (int i = 0; i < cnt[b]; i++) {
    sum[b][i % 5] += w[b][i];
  }
  return;
}
void out() {
  printf("%d\n", cnt[0]);
  for (int i = 0; i < n; i++) printf("%d ", w[0][i]);
  puts("");
  for (int i = 0; i < 5; i++) printf("%d ", sum[0][i]);
  puts("");
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[5];
    scanf("%s", s);
    if (s[0] == 'a')
      tp[i] = 0;
    else if (s[0] == 'd')
      tp[i] = 1;
    else
      tp[i] = 2;
    if (tp[i] != 2) {
      scanf("%d", a + i);
      y[i] = make_pair(a[i], i);
    }
  }
  sort(y, y + n);
  int r = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0 && y[i].first != y[i - 1].first) r++;
    z[y[i].second] = r;
  }
  for (int i = 0; i < n; i++) {
    if (tp[i] == 0)
      add(z[i], a[i]);
    else if (tp[i] == 1)
      del(z[i], a[i]);
    else
      printf("%I64d\n", calc());
  }
  return 0;
}
