#include <stdio.h>
struct student {
char name[100];
int core;
};
int sort;
int compare(void *a, void *b)
{
struct student *c = (struct student *)a;
struct student *d = (struct student *)b;
if (sort == 1) {
return c->core - d->core;
} else {
return d->core - c->core;
}
}
int main(void)
{
struct student *data = NULL;
int num;
int i;
scanf("%d", &num);
scanf("%d", &sort);
data = (struct student *)malloc(sizeof(struct student) * num);
for (i = 0; i < num; i++) {
scanf("%s %d", data[i].name, &data[i].core);
}
qsort(data, num, sizeof(struct student), compare);
for (i = 0; i < num; i++) {
printf("%s %d\n", data[i].name, data[i].core);
}
return 0;
}