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 JAVA):

Solution:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Random;
import java.util.StringTokenizer;


public class Main {
	private static final String TASKNAME = "d";

	class CartesianTree {
	  private final static int RANDSEED = 566;
	  private final Random rand = new Random(RANDSEED);
	  public Node root = null;
	  
	  public long get3rds() {
	  	return root == null ? 0 : root.sums[2];
	  }

	  public void add(int value) {
	      Node v = new Node(value);
	      Node sentinel = new Node(null, null);
	      split(root, sentinel, value);
	      root = merge(merge(sentinel.left, v), sentinel.right);
	  }
	  
	  public void remove(int value) {
	      Node sentinel = new Node(null, null);
	      split(root, sentinel, value);
	      root = merge(sentinel.left, sentinel.right);
	  }

	  public void printAll() {
	      root.debug();
	  }

	  private void split(Node v, Node sentintel, int x) {
	      if (v == null) {
	          return;
	      }
	      if (x == v.x) {
	      	sentintel.left = v.left;
	      	sentintel.right = v.right;
	      }
	      if (x > v.x) {
	          split(v.right, sentintel, x);
	          v.right = sentintel.left;
	          sentintel.left = v;
	      } else if (x < v.x) {
	          split(v.left, sentintel, x);
	          v.left = sentintel.right;
	          sentintel.right = v;
	      }
	      v.recalc();
	  }

	  private Node merge(Node n1, Node n2) {
	      if (n1 == null) {
	          return n2;
	      } else if (n2 == null) {
	          return n1;
	      }
	      if (n1.y > n2.y) {
	          n1.right = merge(n1.right, n2);
	          n1.recalc();
	          return n1;
	      } else {
	          n2.left = merge(n1, n2.left);
	          n2.recalc();
	          return n2;
	      }
	  }

	  public class Node {
	      private int x;
	      private int y;
	      private Node left, right;
	      private final long[] sums;
	      private int size = 0;

	      public Node(int x) {
	          this.x = x;
	          this.y = rand.nextInt();
	          sums = new long[5];
	          sums[0] += x;
	          size = 1;
	      }

	      public void recalc() {
	      	Arrays.fill(sums, 0);
	      	size = 0;
	      	if (left != null) {
	      		for (int i = 0; i < 5; ++i) {
	      			sums[i] += left.sums[i];
	      		}
	      		size += left.size;
	      	}
	      	sums[size % 5] += x;
	      	++size;
	      	if (right != null) {
	      		for (int i = 0; i < 5; ++i) {
	      			sums[(size + i) % 5] += right.sums[i];
	      		}
	      		size += right.size;
	      	}
				}

	      public Node(Node left, Node right) {
	          this.left = left;
	          this.right = right;
	          sums = null;
	      }

	      public void debug() {
	          if (left != null) {
	              left.debug();
	          }
	          System.err.println(x + " s=" + size);
	          if (right != null) {
	              right.debug();
	          }
	      }
	  }
	}
	private void solve() throws Exception {
//		test();
		int n = nextInt();
		CartesianTree tree = new  CartesianTree();
		for (int i = 0; i < n; ++i) {
			String s = nextToken();
			if ("sum".equals(s)) {
				println(tree.get3rds());
//			tree.root.debug();
//			System.err.println("");
			} else {
				int val = nextInt();
				if ("add".equals(s)) {
					tree.add(val);
				} else {
					tree.remove(val);
				}
			}
		}
	}

	private void test() {
		Random random = new Random(566);
		while (true) {
			HashSet<Integer> els = new HashSet<Integer>();
			ArrayList<Integer> list = new  ArrayList<Integer>();
			CartesianTree tree = new CartesianTree();
			for (int i = 0; i < 8; ++i) {
				int v = random.nextInt(15);
				if (els.add(v)) {
					list.add(v);
					tree.add(v);
					System.err.println("add " + v);
				}
			}
			
			Collections.sort(list);
			for (int i = 0; i < 4 && i < list.size(); ++i) {
				int p = random.nextInt(list.size());
				int v = list.get(p);
				list.remove(p);
				tree.remove(v);
				System.err.println("remove " + v);
			}
			long s1 = tree.get3rds();
			long s2 = 0;
			for (int i = 0; i + 2< list.size(); i += 5) {
				s2 += list.get(i + 2);
			}
			System.err.println(s1 + " " + s2);
			if (s1 != s2) {
				throw new AssertionError();
			}
		}
	}

	private void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			writer = new PrintWriter(new OutputStreamWriter(System.out));
//			reader = new BufferedReader(new FileReader(TASKNAME + ".in"));
//			writer = new PrintWriter(new File(TASKNAME + ".out"));

			solve();

			reader.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(13);
		}
	}

	public static void main(String[] args) {
		long time = System.currentTimeMillis();
		Locale.setDefault(Locale.US);
		new Main().run();
		System.err.printf("%.3f\n", (System.currentTimeMillis() - time) * 1e-3);
	}

	private StringTokenizer tokenizer;
	private PrintWriter writer;
	private BufferedReader reader;

	private String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	private int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private void print(Object o) {
		writer.print(o);
	}

	private void println(Object o) {
		writer.println(o);
	}

	private void printf(String format, Object... args) {
		writer.printf(format, args);
	}
}