#include<iostream> #include<algorithm> using namespace std; const int N = 1e5 + 10; struct Stu{ int order; string name; int score; }stu[N]; bool cmp_0(Stu s1,Stu s2) { return s1.score > s2.score; } bool cmp_1(Stu s1,Stu s2) { return s1.score < s2.score; } int main(void) { int n,type; while(cin >> n >> type) { for(int i = 0;i < n;i++) { cin >> stu[i].name >> stu[i].score; } if(type == 0) stable_sort(stu,stu + n,cmp_0); else stable_sort(stu,stu + n,cmp_1); for(int i = 0;i < n;i++) { printf("%s %d\n",stu[i].name.c_str(),stu[i].score); } } return 0; }