from collections import defaultdict


def add_(a):
    global memory, mcnt, hist, i_hist
    memory[a] += 1
    mcnt += 1
    hist.append(a)
    if N < mcnt:
        memory[hist[i_hist]] -= 1
        mcnt -= 1
        i_hist += 1


N, K, Q = map(int, input().split())

memory = defaultdict(int)
mcnt = 0
hist = []
i_hist = 0
for _ in range(Q):
    a = int(input())
    if a == 1:
        s = input()
        add_(s)
    else:
        time = 0
        solve = []
        for _ in range(6):
            t, d = input().split()
            d = int(d)
            if t in memory and 0 < memory[t]:
                time += min(d, K)
            else:
                time += d
            if time <= 60:
                solve.append(t)
        print(len(solve))
        for t in solve:
            add_(t)