#include <iostream>
#include <algorithm>
#define N 100
using namespace std;
struct student
{
    string name;
    int score;
};
bool sortrule(student a, student b)
{
    if (a.score > b.score)
    {
        return true;
    }
    else if (a.score < b.score)
    {
        return false;
    }
    else if (a.score == b.score)
    {
        if (a.name < b.name)
            return true;
        else
            return false;
    }
}
int main()
{
    student stu[N];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].name >> stu[i].score;
    }
    sort(stu, stu + n, sortrule);
    for (int i = 0; i < n; i++)
    {
        cout << stu[i].name << " " << stu[i].score << endl;
    }

    return 0;
}