N,Q=map(int,input().split()) there=[] for i in range(Q): t,*q=input().split() if t=="1": there.append((int(q[1]),q[0])) elif t=="2": N-=int(q[0]) else: N+=int(q[1]) for i in range(len(there)): if there[i][1]==q[0]: there[i]=(there[i][0]+998244353,q[0]) there.sort(reverse=True) while len(there)>N: print(there[-1][1]) there.pop()