# oj t -c "python3 main.py" import sys,math from collections import defaultdict,deque from itertools import combinations,permutations,accumulate,product from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify #from sortedcontainers import SortedList,SortedSet def input():return sys.stdin.readline().rstrip() def ii(): return int(input()) def ms(): return map(int, input().split()) def li(): return list(map(int,input().split())) inf = pow(10,18) mod = 998244353 #////////////////////////////////// N,Q = ms() fix,nofix = [],[] fixed = set() rate = defaultdict(int) for _ in range(Q): f,*s = input().split() if f=="1": s,r = s r = int(r) heappush(nofix,(r,s)) rate[s] = r N -= 1 while N<0: done = False while nofix: _,s = heappop(nofix) if s not in fixed: print(s) done = True break if done==False: _,s = heappop(fix) print(s) N += 1 elif f=="2": x = s[0] x = int(x) N -= x while N<0: done = False while nofix: _,s = heappop(nofix) if s not in fixed: print(s) done = True break if done==False: _,s = heappop(fix) print(s) N += 1 else: s,x = s x = int(x) N += x fixed.add(s) heappush(fix,(rate[s],s))