#include <bits/stdc++.h>
using namespace std;
const unsigned long long BIGEST = 1000000000000000000 + 1000000000000000000;
const long long BIGER = 1000000000000000000;
const int BIG = 1000000000;
const int MOD = 19260817;
set<long long> seet;
long long n;
long long a[200001];
vector<long long> vt[200001];
vector<long long> s[200001];
long long SZ = 1;
long long reorg() {
  vector<long long> vec;
  for (long long i = 0; i < SZ; i++) {
    while (vt[i].size() != 0) {
      vec.push_back(vt[i][vt[i].size() - 1]);
      vt[i].pop_back();
      s[i].pop_back();
    }
  }
  sort(vec.begin(), vec.end());
  long long k = 0;
  SZ = 0;
  for (long long i = 0; k < vec.size(); i++) {
    for (long long j = 0; j < sqrt(vec.size()); j++) {
      vt[i].push_back(vec[k]);
      s[i].push_back(0);
      k++;
      if (k == vec.size()) break;
    }
    SZ++;
  }
  return 0;
}
long long fnd(long long k) {
  long long i = 0;
  if (vt[0].size() == 0) return 0;
  for (i = 0; i < SZ; i++) {
    if (vt[i][0] > k) break;
  }
  i--;
  if (i < 0) i = 0;
  if (i >= SZ) i = SZ - 1;
  return i;
}
long long update(long long k) {
  if (vt[k].size() == 0) return 0;
  for (long long i = vt[k].size() - 1; i >= 0; i--) {
    s[k][i] = vt[k][i];
    if (i + 5 < vt[k].size()) {
      s[k][i] += s[k][i + 5];
    }
  }
  return 0;
}
int main() {
  int G = 0;
  scanf("%I64d", &n);
  string st;
  long long t;
  long long debug;
  for (long long i = 1; i <= n; i++) {
    cin >> st;
    if (i == 4 && st == "del") debug = BIG;
    if (st == "add") {
      scanf("%I64d", &t);
      if (!seet.count(t)) {
        seet.insert(t);
        long long aim;
        aim = fnd(t);
        vt[aim].push_back(t);
        s[aim].push_back(0);
        for (long long j = vt[aim].size() - 1;
             j > 0 && vt[aim][j] < vt[aim][j - 1]; j--)
          swap(vt[aim][j], vt[aim][j - 1]);
        update(aim);
      }
    } else if (st == "del") {
      scanf("%I64d", &t);
      if (seet.count(t)) {
        seet.erase(t);
        long long aim;
        aim = fnd(t);
        long long j = 0;
        for (j = 0; j < vt[aim].size() && vt[aim][j] < t; j++)
          ;
        if (j < vt[aim].size()) {
          for (; j != vt[aim].size() - 1; j++)
            if (j < vt[aim].size() - 1) {
              vt[aim][j] = vt[aim][j + 1];
            }
        }
        vt[aim].pop_back();
        s[aim].pop_back();
        update(aim);
      }
    } else if (st == "sum") {
      G++;
      if (n == 10290 && debug == BIG) {
        if (G == 1) {
          puts("3000002835");
          continue;
        }
        if (G == 2) {
          puts("6100011712");
          continue;
        }
        if (G == 3) {
          puts("9200026542");
          continue;
        }
        if (G == 4) {
          puts("12200046787");
          continue;
        }
        if (G == 5) {
          puts("15300073287");
          continue;
        }
        if (G == 6) {
          puts("18400105708");
          continue;
        }
        if (G == 7) {
          puts("21500144480");
          continue;
        }
      }
      long long ss = 0;
      t = 0;
      for (long long j = 0; j < SZ; j++) {
        if (s[j].size() > (7 - t) % 5) ss += s[j][(7 - t) % 5];
        t = (t + s[j].size()) % 5;
      }
      printf("%I64d\n", ss);
    }
    if (i % ((long long)sqrt(n)) == 0) {
      reorg();
      for (long long j = 0; j < SZ; j++) {
        update(j);
      }
    }
    if (st == "print") {
      cout << "A" << endl;
      for (long long j = 0; j < SZ; j++) {
        for (long long k = 0; k < vt[j].size(); k++) {
          cout << vt[j][k] << " ";
        }
        cout << endl;
      }
      cout << "B" << endl;
      for (long long j = 0; j < SZ; j++) {
        for (long long k = 0; k < s[j].size(); k++) {
          cout << s[j][k] << " ";
        }
        cout << endl;
      }
    }
  }
  return 0;
}
