#include <iostream>
using namespace std;
#include<map>
int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        multimap<int ,string>grade;
        int f;string str;
        for(int i=0;i<a;i++)
       { cin>>str>>f;if(!b)f=100-f;grade.emplace(f,str);}
        for(auto it:grade)
        if(b)cout << it.second << ' '<<it.first<<endl;
        else cout << it.second << ' '<<100-it.first<<endl;
    }
}
// 64 位输出请用 printf("%lld")