#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
    string No;
    string Name;
    double score;
};
int m;
bool cmp(Node& a, Node& b) {
    if (m == 1) {
        return a.No < b.No;
    }
    else if (m == 2) {
        if (a.Name < b.Name)return true;
        if (a.Name == b.Name)return a.No < b.No;
        return false;
    }
    else {
        if (a.score < b.score)return true;
        if (a.score == b.score)return a.No < b.No;
        return false;
    }
}
int main() {
    int n;
    while (cin >> n) {
        if (n == 0)break;
        cin >> m;
        Node* nodes = new Node[n];
        for (int i = 0; i < n; ++i) {
            cin >> nodes[i].No >> nodes[i].Name >> nodes[i].score;
        }
        sort(nodes, nodes + n, cmp);
        cout << "Case:" << endl;
        for (int i = 0; i < n; ++i) {
            cout << nodes[i].No << " " << nodes[i].Name << " " << nodes[i].score << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")