#include <bits/stdc++.h>
#define MAX 1000
using namespace std;

typedef struct student {
    string name;
    int age;
    int grade;
} student;

bool cmp(student a, student b) {
    if (a.grade < b.grade)
        return true;
    else if (a.grade == b.grade && a.name < b.name)
        return true;
    else if (a.grade == b.grade && a.name == b.name && a.age < b.age)
        return true;
    else
        return false;
}

int main() {
    int n;
    cin >> n;
    student data[MAX];
    for (int i = 0; i < n; i++)
        cin >> data[i].name >> data[i].age >> data[i].grade;
    sort(data, data + n, cmp);
    for (int i = 0; i < n; i++)
        cout << data[i].name << " " << data[i].age << " " << data[i].grade << endl;
}