#include <iostream>
#include "algorithm"
using namespace std;
const int N = 1e5 + 10;
typedef struct Student
{
string id, name;
int score;
}Stu;
Stu stu[N];
static bool cmp1(const Stu& a, const Stu& b)
{
return a.id < b.id;
}
static bool cmp2(const Stu& a, const Stu& b)
{
if(a.name == b.name)
return a.id < b.id;
return a.name < b.name;
}
static bool cmp3(const Stu& a, const Stu& b)
{
if(a.score == b.score)
return a.id < b.id;
return a.score < b.score;
}
int main() {
int n, c;
while (cin >> n >> c && n) { // 注意 while 处理多个 case
for(int i = 0; i < n; i ++)
{
cin >> 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);
cout << "Case:\n";
for(int i = 0; i < n; i ++)
{
cout << stu[i].id << ' ' << stu[i].name << ' ' << stu[i].score << endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")