type rec=record
            No:string[6];
            na:string[8];
            gra:shortint
           end;
var a:array[1..100000]of rec;b:string[20];n,i,j,k,y:longint;
function sheng(a,b:rec):boolean;
begin
 case k of
   1:if a.No<b.No then sheng:=true else sheng:=false;
   2:if a.na<b.na then sheng:=true else if a.na>b.na then sheng:=false else if a.No<b.No then sheng:=true else sheng:=false;
   3:if a.gra<b.gra then sheng:=true else if a.gra>b.gra then sheng:=false else if a.No<b.No then sheng:=true else sheng:=false;
  end;
end;
procedure sort(l,r:longint);
var i,j:longint;x,t:rec;
begin
 i:=l;j:=r;x:=a[(i+j)div 2];
 repeat
  while sheng(a[i],x) do inc(i);
  while sheng(x,a[j]) do dec(j);
  if i<=j then
  begin
   t:=a[i];a[i]:=a[j];a[j]:=t;
   inc(i);dec(j)
  end
 until i>j;
 if j>l then sort(l,j);
 if i<r then sort(i,r)
end;
begin
 read(n);j:=0;
 while n<>0 do
  begin
   inc(j);
   readln(k);
   for i:=1 to n do
    begin
     readln(b);
     a[i].No:=copy(b,1,6);
     delete(b,1,7);
     a[i].na:=copy(b,1,pos(' ',b)-1);
     delete(b,1,pos(' ',b));
     val(b,a[i].gra,y)
    end;
   sort(1,n);
   writeln('Case ',j,':');
   for i:=1 to n do
    writeln(a[i].No,' ',a[i].na,' ',a[i].gra);
   read(n)
  end
end.
/**************************************************************
	Problem: 2200
	User: admin
	Language: Pascal
	Result: Wrong Answer
****************************************************************/