#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student{
    string code;
    string name;
    int score;
    Student(string c,string n,int s):code(c),name(n),score(s){}
};

bool cmp1(Student a, Student b){
    return a.code < b.code;
}
bool cmp2(Student a, Student b){
    if(a.name==b.name){
        return a.code<b.code;
    }else{
        return a.name<b.name;
    }
    
}
bool cmp3(Student a, Student b){
    if(a.score==b.score){
        return a.code<b.code;
    }else{
        return a.score<b.score;
    }
}

int main() {
    int n,c,order=1;
    while(scanf("%d %d",&n,&c)!=EOF){
        vector <Student> s;
        vector <Student>::iterator it;
        int score;
        string name,code;
        for(int i=0;i<n;i++){
            cin>>code>>name>>score;
            s.push_back(Student(code,name,score));
        }
        if(c==1){
            sort(s.begin(),s.end(),cmp1);
        }else if(c==2){
             sort(s.begin(),s.end(),cmp2);
        } else if(c==3){
             sort(s.begin(),s.end(),cmp3);
        }
        printf("Case:\n",order++);
        for(it=s.begin();it!=s.end();it++){
            cout<<it->code<<" "<<it->name<<" "<<it->score<<endl;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")