#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxv = 1e5 + 5;
struct Stu{
int id;
char name[10];
int score;
}stu[maxv];
bool cmp1(Stu a, Stu b){
return a.id < b.id;
}
bool cmp2(Stu a, Stu b){
int s = strcmp(a.name,b.name);
if(s!=0) return s< 0;
else return a.id < b.id;
}
bool cmp3(Stu a, Stu b){
if(a.score != b.score) return a.score < b.score;
else return a.id < b.id;
}
int main()
{
int n,c;
scanf("%d%d",&n,&c);
for(int i = 0; i < n; i++){
scanf("%d %s %d", &stu[i].id,&stu[i].name,&stu[i].score);
}
if(c==1){
sort(stu,stu+n,cmp1);
}else if(c==2){
sort(stu,stu+n,cmp2);
}else
sort(stu,stu+n,cmp3);
for(int i =0; i < n; i++){
printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].score);
}
//cout << "Hello world!" << endl;
return 0;
}