if __name__ == "__main__":
line = input().split()
n = int(line[0])
m = int(line[1])
c = int(line[2])
core = [-1] * 10001
res = []
for i in range(m):
res.append([])
total = [0] * m
for i in range(n):
line = input().split()
type = int(line[0])
user = int(line[1])
time = int(line[2])
deadline = int(line[3])
if core[user] == -1:
for j in range(m):
if core[user] == -1 or total[j] < total[core[user]]:
core[user] = j
res[core[user]].append([type, user])
total[core[user]] += time
for i in range(m):
print(len(res[i]), end=' ')
for j in range(len(res[i])):
print(res[i][j][0], res[i][j][1], end=' ')
print()