import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class D {
    public static List<Long> newAl = new ArrayList<>();
    public static List<Long> prevAl = new ArrayList<>();
    public static List<Long> currAl = new ArrayList<>();
    public static Set<Long> toDelete = new HashSet<>();
    public static long sum = 0;
    
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		long N = Long.parseLong(br.readLine());
		while(N-- > 0) {
			String oprt = br.readLine();
			if(oprt.charAt(0) == 'a') {
				newAl.add(Long.parseLong(oprt.split(" ")[1]));
			}else if(oprt.charAt(0) == 'd') {
				toDelete.add(Long.parseLong(oprt.split(" ")[1]));
			}else if(oprt.charAt(0) == 's') {
				Collections.sort(newAl);
				mergeLists();
				System.out.println(sum);
				prevAl = currAl;
				currAl = new ArrayList<>();
				newAl = new ArrayList<>();
				sum = 0;
			}
		
		}
	
	}
	
	public static void mergeLists() {
		int i = 0 , j = 0 , k = 0;
		while(i < prevAl.size() && j < newAl.size()) {
			if(prevAl.get(i) < newAl.get(j)) {
				if(!toDelete.contains(prevAl.get(i))) {
					currAl.add(k, prevAl.get(i));
					k++;
				}
			   i++;
			}else {
				if(!toDelete.contains(newAl.get(j))) {
					currAl.add(k, newAl.get(j));
					k++;
				}
			   j++;
			}
		   if(k % 5 == 3) sum += currAl.get(k - 1);
		}
		if(i >= prevAl.size()) {
			while(j < newAl.size()) {
				if(!toDelete.contains(newAl.get(j))) {
					currAl.add(k, newAl.get(j));
					if(k % 5 == 3) sum += currAl.get(k - 1);
					k++;
				}
				j++;
			}
		}
		if(j >= newAl.size()) {
			while(i < prevAl.size()) {
				if(!toDelete.contains(prevAl.get(i))) {
					currAl.add(k, prevAl.get(i));
					if(k % 5 == 3) sum += currAl.get(k - 1);
					k++;
				}
			    i++;
			}
		}
	}
	
}
