#include <bits/stdc++.h>
using namespace std;
void readline(string &s) {
  s = "";
  char c;
  for (c = ((char)fgetc(stdin)); c != '\n'; s += c, c = ((char)fgetc(stdin)))
    ;
}
void readstr(string &s) {
  s = "";
  char c;
  for (c = ((char)fgetc(stdin)); c && c != ' ' && c != '\n';
       s += c, c = ((char)fgetc(stdin)))
    ;
}
void readstr(char *s) {
  int len = 0;
  char c;
  for (c = ((char)fgetc(stdin)); c && c != ' ' && c != '\n';
       s[len++] = c, c = ((char)fgetc(stdin)))
    ;
}
void printstr(const string &s) {
  int len = (int)s.size();
  for (int(i) = (0); (i) <= (len - 1); i++) fputc((s[i]), stdout);
}
void to_lower(string &s) {
  int len = (int)s.size();
  for (int(i) = (0); (i) <= (len - 1); i++)
    if (((s[i]) >= 'A' && (s[i]) <= 'Z')) s[i] = s[i] - 'A' + 'a';
}
void to_upper(string &s) {
  int len = (int)s.size();
  for (int(i) = (0); (i) <= (len - 1); i++)
    if (((s[i]) >= 'a' && (s[i]) <= 'z')) s[i] = s[i] - 'a' + 'A';
}
long long readll() {
  char c;
  long long ans = 0, x = 1;
  for (c = ((char)fgetc(stdin)); !((c) >= '0' && (c) <= '9') && c != '-';
       c = ((char)fgetc(stdin)))
    ;
  if (c == '-') x = -1, c = ((char)fgetc(stdin));
  for (; ((c) >= '0' && (c) <= '9');
       ans = ans * 10 + c - '0', c = ((char)fgetc(stdin)))
    ;
  return ans * x;
}
void printll(long long x) {
  int a[25], cnt = 0;
  if (x == 0) {
    fputc(('0'), stdout);
    return;
  }
  if (x < 0) fputc(('-'), stdout), x = -x;
  for (; x; a[cnt++] = x - x / 10 * 10, x /= 10)
    ;
  for (int(i) = (cnt - 1); (i) >= (0); i--) fputc((a[i] + '0'), stdout);
}
struct command {
  char c;
  int x;
  command(char _c = 'q', int _x = -1) { c = _c, x = _x; }
} cmd[100005];
int Minus(int px) { return (px + 5 - 1) % 5; }
int Add(int px) { return (px + 1) % 5; }
int n, cnt[5] = {0};
long long a[5][100005], tmp[100005];
int main() {
  for (int(i) = (0); (i) <= (4); i++)
    for (int(j) = (0); (j) <= (100000); j++) a[i][j] = 10000000000000000ll;
  n = ((int)readll());
  for (int(tc) = (1); (tc) <= (n); tc++) {
    char c = ((char)fgetc(stdin));
    while (c != 's' && c != 'd' && c != 'a') c = ((char)fgetc(stdin));
    if (c != 's')
      cmd[tc] = command(c, ((int)readll()));
    else
      cmd[tc] = command(c);
  }
  for (int(tc) = (1); (tc) <= (n); tc++) {
    if (cmd[tc].c == 's') {
      long long sum = 0;
      for (int(i) = (0); (i) <= (cnt[2] - 1); i++) sum += a[2][i];
      printll((long long)(sum));
      fputc(('\n'), stdout);
    } else {
      int px = -1, py = -1;
      int poss[5];
      for (int(i) = (0); (i) <= (4); i++) {
        a[i][cnt[i]] = 10000000000000000ll;
        int pos =
            lower_bound(a[i], a[i] + cnt[i] + (cmd[tc].c == 'a'), cmd[tc].x) -
            a[i];
        poss[i] = pos;
        if (pos < cnt[i] + (cmd[tc].c == 'a') &&
            (px == -1 || a[i][pos] == cmd[tc].x || pos >= py) &&
            a[px][py] != cmd[tc].x) {
          px = i, py = pos;
        }
      }
      if (cmd[tc].c == 'd') {
        poss[px]++;
        int cnttmp = cnt[Add(px)] - poss[Add(px)];
        for (int(j) = (poss[Add(px)]); (j) <= (cnt[Add(px)] - 1); j++)
          tmp[j - poss[Add(px)]] = a[Add(px)][j];
        int j = Add(px);
        cnt[j] = poss[j];
        for (int(k) = (1); (k) <= (4); k++) {
          for (int(kk) = (poss[Add(j)]); (kk) <= (cnt[Add(j)] - 1); kk++)
            a[j][cnt[j]++] = a[Add(j)][kk];
          j = Add(j);
          cnt[j] = poss[j];
        }
        cnt[px]--;
        for (int(k) = (0); (k) <= (cnttmp - 1); k++) a[px][cnt[px]++] = tmp[k];
      } else {
        px = Add(px);
        int cnttmp = cnt[Minus(px)] - poss[Minus(px)];
        for (int(j) = (poss[Minus(px)]); (j) <= (cnt[Minus(px)] - 1); j++)
          tmp[j - poss[Minus(px)]] = a[Minus(px)][j];
        int j = Minus(px);
        cnt[j] = poss[j];
        for (int(k) = (1); (k) <= (4); k++) {
          for (int(kk) = (poss[Minus(j)]); (kk) <= (cnt[Minus(j)] - 1); kk++)
            a[j][cnt[j]++] = a[Minus(j)][kk];
          j = Minus(j);
          cnt[j] = poss[j];
        }
        a[px][cnt[px]++] = cmd[tc].x;
        for (int(k) = (0); (k) <= (cnttmp - 1); k++) a[px][cnt[px]++] = tmp[k];
      }
    }
  }
  return 0;
}
