#include <algorithm>
#include <iostream>

using namespace std;
struct Student {
    string name;
    int age;
    int grade;
};
Student student[1001];
bool cmp(Student a, Student b) {
    if (a.grade < b.grade) return true;
    if (a.grade == b.grade && a.name < b.name) return true;
    if (a.grade == b.grade && a.name == b.name && a.age < b.age) return true;
    return false;
}
int main() {
    int n;
    while (cin >> n) {
        for (int i = 0; i < n; ++i) {
            cin >> student[i].name >> student[i].age >> student[i].grade;
        }
        sort(student, student + n, cmp);
        for (int i = 0; i < n; ++i) {
            cout << student[i].name << " " << student[i].age << " " << student[i].grade <<
                 endl;
        }
    }
}