using System;
using System.Linq;
using System.Collections.Generic;
class main
{
public static void Main(string[] args)
{
var line = Console.ReadLine().Split(' ');
var n = Int32.Parse(line[0]);
var m = Int32.Parse(line[1]);
var c = Int32.Parse(line[2]);
var core = new int[10001];
Array.Fill(core, -1);
var res = new List<List<int>>[m];
for(int i = 0; i < m; ++i)
{
res[i] = new List<List<int>>();
}
var total = new int[m];
Array.Fill(total, 0);
for(int i = 0; i < n; i++)
{
line = Console.ReadLine().Split(' ');
var type = Int32.Parse(line[0]);
var user = Int32.Parse(line[1]);
var time = Int32.Parse(line[2]);
var deadline = Int32.Parse(line[3]);
if(core[user] == -1)
{
for(int j = 0; j < m; ++j)
{
if(core[user] == -1 || total[j] < total[core[user]])
{
core[user] = j;
}
}
}
res[core[user]].Add(new int[]{type, user}.ToList());
total[core[user]] += time;
}
for(int i = 0; i < m; ++i)
{
System.Console.Write(res[i].Count);
for(int j = 0; j < res[i].Count; ++j)
{
System.Console.Write(" {0} {1}", res[i][j][0], res[i][j][1]);
}
System.Console.WriteLine();
}
}
}