#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
    string id, name;
    int score;
};
int main() {
    int n, c;
    while (cin >> n >> c) {
        Student student[n];
        for (int i = 0; i < n; i++) cin >> student[i].id >> student[i].name >> student[i].score;
        switch (c) {
            case 1:
                sort(student, student + n, [](Student a, Student b) { return a.id < b.id; });
                break;
            case 2:
                sort(student, student + n,
                     [](Student a, Student b) { return a.name == b.name ? a.id < b.id : a.name < b.name; });
                break;
            case 3:
                sort(student, student + n,
                     [](Student a, Student b) { return a.score == b.score ? a.id < b.id : a.score < b.score; });
                break;
            default:
                break;
        }
        cout << "Case:\n";
        for_each(student, student + n, [](Student a) { cout << a.id << " " << a.name << " " << a.score << endl; });
    }
    return 0;
}