#include<bits/stdc++.h> using namespace std; struct Student{ int idx; string name; int score; }; int main(){ int n; int flag; while(cin>>n>>flag){ vector<Student> scores(n); for(int i=0;i<n;i++){ scores[i].idx=i; cin>>scores[i].name>>scores[i].score; } sort(scores.begin(),scores.end(), [flag](Student &s1,Student &s2)->bool{ //升序 if(flag){ if(s1.score<s2.score) return true; else if(s1.score==s2.score) return s1.idx<=s2.idx; }else{ if(s1.score>s2.score) return true; else if(s1.score==s2.score) return s1.idx<=s2.idx; } return false; }); for(int i=0;i<n;i++) cout<<scores[i].name<<" "<<scores[i].score<<endl; } return 0; }