#include <stdio.h>
struct score {
char name[10];
int s;
};
int main() {
int n = 0;
int x = 0;
scanf("%d", &n);
scanf("%d", &x);
struct score str[200];
for (int i = 0; i < n; i++)
scanf("%s%d", str[i].name, &str[i].s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (x == 1) {
if (str[j].s > str[j + 1].s) {
struct score tmp = str[j];
str[j] = str[j + 1];
str[j + 1] = tmp;
}
} else if (x == 0)
{
if (str[j].s < str[j + 1].s) {
struct score tmp = str[j];
str[j] = str[j + 1];
str[j + 1] = tmp;
}
}
}
}
for (int i = 0; i < n; i++)
printf("%s %d\n", str[i].name, str[i].s);
return 0;
}