#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{
char num[10];
char name[10];
int grade;
}s[100002];
int cmp1(student s1,student s2)//case 1 按照学号递增
{
return strcmp(s1.num,s2.num)<0;
}
int cmp2(student s1,student s2)//case 2 姓名的非递减字典
{
if(strcmp(s1.name,s2.name)==0) return strcmp(s1.num,s2.num)<0;
else return strcmp(s1.name,s2.name)<0;
}
int cmp3(student s1,student s2)//case3
{
if(s1.grade==s2.grade) return strcmp(s1.num,s2.num)<0;
else return s1.grade<s2.grade;
}
int main()
{
int N,C;
int count=0;
while(scanf("%d %d",&N,&C)!=EOF)
{
count++;
if(N==0 && C==0) return 0;
int i;
for(i=0;i<N;i++)
scanf("%s %s %d",s[i].num,s[i].name,&s[i].grade);
switch(C)
{
case 1:{
sort(s,s+N,cmp1);
break;
}
case 2:{
sort(s,s+N,cmp2);
break;
}
case 3:{
sort(s,s+N,cmp3);
break;
}
}
printf("Case %d:\n",count);
for(i=0;i<N;i++)
printf("%s %s %d\n",s[i].num,s[i].name,s[i].grade);
}
return 0;
}