from collections import Counter
import heapq


Q = int(input())
queries = [tuple(map(int, input().split())) for _ in range(Q)]

S = Counter()  # type: ignore
maxS = []  # type: ignore
minS = []  # type: ignore

for query in queries:
    cmd = query[0]
    if cmd == 1:
        _, x = query
        if S[x] == 0:
            heapq.heappush(maxS, -x)
            heapq.heappush(minS, x)
        S[x] += 1
    elif cmd == 2:
        _, x, c = query
        S[x] -= min(c, S[x])
    elif cmd == 3:
        while S[-maxS[0]] == 0:
            heapq.heappop(maxS)
        while S[minS[0]] == 0:
            heapq.heappop(minS)
        print(-maxS[0] - minS[0])
    else:
        assert False
