Problem: In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.

A sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as 

<image>

The <image> operator stands for taking the remainder, that is <image> stands for the remainder of dividing x by y.

To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

Input

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.

Then each of n lines contains the description of one of the three operations: 

  * add x — add the element x to the set; 
  * del x — delete the element x from the set; 
  * sum — find the sum of medians of the set. 



For any add x operation it is true that the element x is not included in the set directly before the operation.

For any del x operation it is true that the element x is included in the set directly before the operation.

All the numbers in the input are positive integers, not exceeding 109.

Output

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).

Examples

Input

6
add 4
add 5
add 1
add 2
add 3
sum


Output

3


Input

14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum


Output

5
11
13

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
const int M = 5;
const int H = M / 2;
const long long LINF = 1LL << 62;
template <typename T, const int BUFSIZE>
struct TreapMed {
  struct Node {
    T key, minkey, sums[M];
    int fix, size;
    Node *left, *right;
    void print(int k) {
      printf("%d: key=%lld, minkey=%lld, fix=%d, size=%d: (", k, key, minkey,
             fix, size);
      for (int i = 0; i < M; i++) {
        if (i) putchar(',');
        printf("%lld", sums[i]);
      }
      putchar(')');
      putchar('\n');
    }
    void print() { print(0); }
  };
  typedef Node* node;
  node root, nullnode, buf;
  Node buf0[BUFSIZE], buf1;
  const T TINF;
  TreapMed(T tinf) : TINF(tinf) {
    nullnode = &buf1;
    nullnode->left = nullnode->right = nullnode;
    nullnode->key = nullnode->minkey = TINF;
    nullnode->fix = nullnode->size = 0;
    memset(nullnode->sums, 0, sizeof(nullnode->sums));
    clear();
    srand(time(NULL));
  }
  void clear() {
    buf = buf0;
    root = nullnode;
  }
  int size() { return root->size; }
  T medsum() { return root->sums[H]; }
  void update(node& t) {
    t->size = 1 + t->left->size + t->right->size;
    t->minkey = t->key;
    if (t->minkey > t->left->minkey) t->minkey = t->left->minkey;
    if (t->minkey > t->right->minkey) t->minkey = t->right->minkey;
    memcpy(t->sums, t->left->sums, sizeof(t->sums));
    int sl = t->left->size % M;
    t->sums[sl++] += t->key;
    for (int i = 0; i < M; i++) t->sums[(i + sl) % M] += t->right->sums[i];
  }
  node gen_node(T x) {
    node t = buf++;
    t->left = t->right = nullnode;
    t->key = t->minkey = x;
    t->fix = rand();
    t->size = 1;
    memset(t->sums, 0, sizeof(t->sums));
    t->sums[0] = x;
    return t;
  }
  void rot_l(node& k1) {
    node k2 = k1->right;
    k1->right = k2->left;
    k2->left = k1;
    update(k1);
    update(k2);
    k1 = k2;
  }
  void rot_r(node& k1) {
    node k2 = k1->left;
    k1->left = k2->right;
    k2->right = k1;
    update(k1);
    update(k2);
    k1 = k2;
  }
  void append(node& t, T x) {
    if (t == nullnode)
      t = gen_node(x);
    else {
      append(t->right, x);
      update(t);
      if (t->right->fix > t->fix) rot_l(t);
    }
  }
  void append(T x) { append(root, x); }
  void insertat(node& t, int i, T x) {
    if (t == nullnode)
      t = gen_node(x);
    else {
      int lsize = t->left->size;
      if (i == lsize) {
        append(t->left, x);
        update(t);
        if (t->left->fix > t->fix) rot_r(t);
      } else if (i < lsize) {
        insertat(t->left, i, x);
        update(t);
        if (t->left->fix > t->fix) rot_r(t);
      } else {
        insertat(t->right, i - (lsize + 1), x);
        update(t);
        if (t->right->fix > t->fix) rot_l(t);
      }
    }
  }
  void insertat(int i, T x) { insertat(root, i, x); }
  void insert(node& t, T x) {
    if (t == nullnode)
      t = gen_node(x);
    else {
      if (t->key == x) return;
      if (x < t->key) {
        insert(t->left, x);
        update(t);
        if (t->left->fix > t->fix) rot_r(t);
      } else {
        insert(t->right, x);
        update(t);
        if (t->right->fix > t->fix) rot_l(t);
      }
    }
  }
  void insert(T x) { insert(root, x); }
  void set(node& t, int i, T x) {
    if (t != nullnode) {
      int lsize = t->left->size;
      if (i == lsize) {
        t->key = t->minkey = x;
        update(t);
      } else if (i < lsize) {
        set(t->left, i, x);
        update(t);
      } else {
        set(t->right, i - (lsize + 1), x);
        update(t);
      }
    }
  }
  void set(int i, T x) { set(root, i, x); }
  void remove_node(node& t) {
    if (t == nullnode) return;
    if (t->left == nullnode || t->right == nullnode) {
      if (t->left == nullnode)
        t = t->right;
      else
        t = t->left;
    } else {
      if (t->left->fix < t->right->fix) {
        rot_l(t);
        remove_node(t->left);
        update(t);
      } else {
        rot_r(t);
        remove_node(t->right);
        update(t);
      }
    }
  }
  T removeat(node& t, int i) {
    T ret = TINF;
    if (t != nullnode) {
      int lsize = t->left->size;
      if (i == lsize) {
        ret = t->key;
        remove_node(t);
      } else if (i < lsize) {
        ret = removeat(t->left, i);
        update(t);
      } else {
        ret = removeat(t->right, i - (lsize + 1));
        update(t);
      }
    }
    return ret;
  }
  T removeat(int i) { return removeat(root, i); }
  void remove(node& t, T x) {
    if (t != nullnode) {
      if (t->key == x)
        remove_node(t);
      else if (x < t->key) {
        remove(t->left, x);
        update(t);
      } else {
        remove(t->right, x);
        update(t);
      }
    }
  }
  void remove(T x) { remove(root, x); }
  int remove_range(node& t, int r0, int r1) {
    if (t == nullnode) return 0;
    if (r1 <= 0 || t->size <= r0) return 0;
    if (r0 <= 0 && t->size <= r1) {
      int n = t->size;
      t = nullnode;
      return n;
    }
    int lsize = t->left->size;
    int n = remove_range(t->left, r0, r1) +
            remove_range(t->right, r0 - (lsize + 1), r1 - (lsize + 1));
    t->size = 1 + t->left->size + t->right->size;
    if (r0 <= lsize && lsize < r1) {
      n++;
      remove_node(t);
    }
    return n;
  }
  int remove_range(int r0, int r1) { return remove_range(root, r0, r1); }
  T min_range(node& t, int r0, int r1) {
    if (t == nullnode) return TINF;
    if (r1 <= 0 || t->size <= r0) return TINF;
    if (r0 <= 0 && t->size <= r1) return t->minkey;
    int lsize = t->left->size;
    T minval = TINF;
    if (r0 <= lsize && lsize < r1) minval = t->key;
    if (r0 <= lsize) {
      T minl = min_range(t->left, r0, r1);
      if (minval > minl) minval = minl;
    }
    if (lsize + 1 < r1) {
      T minr = min_range(t->right, r0 - (lsize + 1), r1 - (lsize + 1));
      if (minval > minr) minval = minr;
    }
    return minval;
  }
  T min_range(int r0, int r1) { return min_range(root, r0, r1); }
  node find(T x) {
    node t = root;
    while (t != nullnode) {
      if (t->key == x) return t;
      if (x < t->key)
        t = t->left;
      else
        t = t->right;
    }
    return (node)NULL;
  }
  T findat(int i) {
    node t = root;
    while (t != nullnode) {
      int lsize = t->left->size;
      if (i == lsize) return t->key;
      if (i < lsize)
        t = t->left;
      else {
        i -= lsize + 1;
        t = t->right;
      }
    }
    return TINF;
  }
  int upper_bound(T x) {
    node t = root;
    int k = 0;
    while (t != nullnode) {
      if (t->key <= x) {
        k += t->left->size + 1;
        t = t->right;
      } else {
        t = t->left;
      }
    }
    return k;
  }
  void print(node t, int k, int indent) {
    if (t == nullnode) return;
    if (t != NULL) {
      for (int i = 0; i < indent; i++) cout << "  ";
      t->print(k + t->left->size);
      print(t->left, k, indent + 1);
      print(t->right, k + t->left->size + 1, indent + 1);
    }
  }
  void print() { print(root, 0, 0); }
  void inorder(node t) {
    if (t != nullnode && t != NULL) {
      inorder(t->left);
      cout << t->key << ' ';
      inorder(t->right);
    }
  }
  void inorder() {
    inorder(root);
    cout << endl;
  }
};
enum { ADD, DEL, SUM };
TreapMed<long long, MAX_N> trp(LINF);
int main() {
  int n;
  scanf("%d", &n);
  while (n--) {
    char op[8];
    scanf("%s", op);
    if (op[0] == 'a') {
      int x;
      scanf("%d", &x);
      trp.insert(x);
    } else if (op[0] == 'd') {
      int x;
      scanf("%d", &x);
      trp.remove(x);
    } else if (op[0] == 's') {
      printf("%lld\n", trp.medsum());
    }
  }
  return 0;
}