import java.util.*;
public class quickstart
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int c = in.nextInt();
int[] core = new int[10001];
Arrays.fill(core, -1);
List<List<Integer>>[] res = new ArrayList[m];
for(int i = 0; i < m; ++i)
{
res[i] = new ArrayList<List<Integer>>();
}
int[] total = new int[m];
Arrays.fill(total, 0);
for(int i = 0; i < n; i++)
{
int type = in.nextInt();
int user = in.nextInt();
int time = in.nextInt();
int deadline = in.nextInt();
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(Arrays.asList(type, user));
total[core[user]] += time;
}
for(int i = 0; i < m; ++i)
{
System.out.print(res[i].size());
for(int j = 0; j < res[i].size(); ++j)
{
System.out.print(" " + res[i].get(j).get(0) + " " + res[i].get(j).get(1));
}
System.out.println();
}
}
}