N,Q=map(int,input().split())
there=[]
for _ 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+998244353,q[0])
    there.sort(reverse=True)
    goodbye=[]
    while len(there)>N:
        goodbye.append((there[-1][0]%998244353,there[-1][1]))
        there.pop()
    goodbye.sort()
    for i in goodbye:
        print(i[1])