#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 自定义比较函数
struct user{
    string name;
    int score;
};

bool myIncrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score < stTmpUser2.score;
}

bool myDecrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score > stTmpUser2.score;
}

int main() {
    int n, bisIncrease;
    cin >> n >> bisIncrease; 
    vector<struct user> userForm;
    struct user stTmpUser;
    for(int i = 0; i < n; i++){
        cin >> stTmpUser.name >> stTmpUser.score;
        userForm.push_back(stTmpUser);
    }

    if(bisIncrease){
        stable_sort(userForm.begin(), userForm.end(), myIncrease);
    }
    else{
        stable_sort(userForm.begin(), userForm.end(), myDecrease );
    }
    
    for(int i = 0; i < n; i++){
        cout << userForm[i].name << " " << userForm[i].score << endl;
    }

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