#include<bits/stdc++.h> using namespace std; struct Student{ string name; int score; int flag; }arr[100001]; bool cmp1(Student x,Student y){ if(x.score != y.score) return x.score > y.score; else return x.flag < y.flag; } bool cmp2(Student x,Student y){ if(x.score != y.score) return x.score < y.score; else return x.flag < y.flag; } int main(){ int n; while(cin>>n){ int t; cin>>t; for(int i=0;i<n;i++) { cin>>arr[i].name>>arr[i].score; arr[i].flag = i; } if(t == 0) sort(arr,arr+n,cmp1); else sort(arr,arr+n,cmp2); for(int i=0;i<n;i++) cout<<arr[i].name<<" "<<arr[i].score<<endl; } return 0; }