#include <bits/stdc++.h>
using namespace std;
struct sc{
string name;
int score;
};
bool cmp1(sc a,sc b){
return a.score<b.score;
}
bool cmp2(sc a,sc b){
return a.score>b.score;
}
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
vector<sc> v;
string s;int t;
while(a--)
{
cin>>s>>t;
v.push_back({s,t});
}
if(b)
stable_sort(v.begin(),v.end(),cmp1);
else
stable_sort(v.begin(),v.end(),cmp2);
for(auto i:v)
{
cout<<i.name<<" "<<i.score<<endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")