#include <iostream>
#include <string>
using namespace std;

class student {
public:
    string name;
    int grade;
    bool operator<(const student& t) {
        return this->grade < t.grade;
    };
    bool operator>(const student& t) {
        return this->grade > t.grade;
    };
    friend istream& operator>>(istream& in, student& t) {
        in >> t.name >> t.grade;
        return in;
    }
    friend ostream& operator<<(ostream& out, const student& t) {
        out << t.name << " " << t.grade << endl;
        return out;
    }
    student operator=(const student& i) {
        this->name = i.name;
        this->grade = i.grade;
        return *this;
    }
};

void sort(student* students, int n, bool flag)
{
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (flag) {//升序排
                if (students[j] > students[j + 1]) {
                    student temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
            else {//降序排
                if (students[j] < students[j + 1]) {
                    student temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
        }
    }
}

int main() {
    int n;
    bool flag;
    while(cin >> n >> flag){
        student* students = new student[n];
        for (int i = 0; i < n; i++)
            cin >> students[i];

        sort(students, n, flag);

        for (int i = 0; i < n; i++)
            cout << students[i];

        delete[] students;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")