#include<stdio.h>
#include<string.h>
typedef struct Node{
char name[100];
int grade;
}Node;
int main()
{
int n,sj;
while(EOF != scanf("%d%d",&n,&sj))
{
int i,j,k;
Node narr[200];
Node temp;
for(i=0; i<n;i++)
{
scanf("%s%d",narr[i].name,&narr[i].grade);
}
if(sj==0) //降序
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(narr[j].grade < narr[j+1].grade)
{
temp.grade = narr[j].grade; strcpy(temp.name, narr[j].name);
narr[j].grade = narr[j+1].grade; strcpy(narr[j].name, narr[j+1].name);
narr[j+1].grade = temp.grade; strcpy(narr[j+1].name, temp.name);
}
}
}
}else if(sj == 1)
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(narr[j].grade > narr[j+1].grade)
{
temp.grade = narr[j].grade; strcpy(temp.name, narr[j].name);
narr[j].grade = narr[j+1].grade; strcpy(narr[j].name, narr[j+1].name);
narr[j+1].grade = temp.grade; strcpy(narr[j+1].name, temp.name);
}
}
}
}
for(i=0;i<n;i++)
{
printf("%s %d\n",narr[i].name,narr[i].grade);
}
}
return 0;
}