#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct student{
char name[100];
int score;
int rank;
};
bool compare0(student lhs,student rhs){
if(lhs.score>rhs.score) return true;
if(lhs.score==rhs.score&&lhs.rank<rhs.rank) return true;
else return false;
}
bool compare1(student lhs,student rhs){
if(lhs.score<rhs.score) return true;
if(lhs.score==rhs.score&&lhs.rank<rhs.rank) return true;
else return false;
}
int main(){
int N,i;
scanf("%d",&N);
scanf("%d",&i);
vector<student> vec(N);
for(int j=0;j<N;j++){
scanf("%s %d",vec[j].name,&vec[j].score);
vec[j].rank=j;
}
if(i==0){ //降序
sort(vec.begin(),vec.end(),compare0);
}
else { //升序
sort(vec.begin(),vec.end(),compare1);
}
for(int n=0;n<N;n++){
printf("%s %d\n",vec[n].name,vec[n].score);
}
return 0;
}