#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define maxn 100010
using namespace std;
typedef struct Case{
    string no;
    string name;
    int sco;
}Ca;

Ca c[maxn];
bool cmp1(Ca c1,Ca c2)
{
    return c1.no<c2.no;
}

bool cmp2(Ca c1,Ca c2)
{
    if(c1.name!=c2.name)return c1.name<c2.name;
    return c1.no<c2.no;
}
bool cmp3(Ca c1,Ca c2)
{
    if(c1.sco!=c2.sco)return c1.sco<c2.sco;
    return c1.no<c2.no;
}

int main() {
    int N,C;
    while(cin>>N>>C)
    {
        for(int i=0;i<N;i++)
        {
            cin>>c[i].no>>c[i].name>>c[i].sco;
        }
        if(C==1){
            sort(c,c+N,cmp1);
            cout<<"Case:"<<endl;
            for(int i=0;i<N;i++)
                cout<<c[i].no<<" "<<c[i].name<<" "<<c[i].sco<<endl;
        }
        else if(C==2){
            sort(c,c+N,cmp2);
            cout<<"Case:"<<endl;
            for(int i=0;i<N;i++)
                cout<<c[i].no<<" "<<c[i].name<<" "<<c[i].sco<<endl;
        }
        else if(C==3){
            sort(c,c+N,cmp3);
            cout<<"Case:"<<endl;
            for(int i=0;i<N;i++)
                cout<<c[i].no<<" "<<c[i].name<<" "<<c[i].sco<<endl;
        }
    }
}
// 64 位输出请用 printf("%lld")