#include<cstdio>
#include<algorithm>
using namespace std;
struct Student{
char name[50];
int grade;
int sequence;//记录录入的顺序
};
bool comp1(Student lhs,Student rhs){
if (lhs.grade < rhs.grade){
return true;
}
else if (lhs.grade ==rhs.grade && lhs.sequence < rhs.sequence){
return true;
}
else{
return false;
}
}
bool comp0(Student lhs,Student rhs){
if (lhs.grade > rhs.grade){
return true;
}
else if(lhs.grade ==rhs.grade && lhs.sequence < rhs.sequence){
return true;
}
else{
return false;
}
}
int main(){
int N;//数组大小
int order;//升序or降序
Student arr[1000];
while (scanf("%d%d",&N,&order) != EOF){
int sequence = 0;//sequence 用于记录序号
for (int i = 0; i < N; ++i){
scanf("%s %d",arr[i].name,&arr[i].grade);
arr[i].sequence = sequence;
++sequence;//每次读取后sequence自增 从小到大
}
if (0==order ){
sort(arr,arr+N,comp0);
}
else{
sort(arr,arr+N,comp1);
}
for (int i = 0; i < N; ++i){
printf("%s %d\n",arr[i].name,arr[i].grade);
}
}
}