#include <stdio.h>

struct Student {
    char name[20];
    int score;
} stu[100];

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        int c;//排序方式 0降序 1升序
        struct Student temp;
        scanf("%d", &c);
        for (int i = 0; i < n; i++) {
            scanf("%s %d", stu[i].name, &stu[i].score);
        }//存储输入内容
        if (c == 0) { //降序
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n - i ; j++) {
                    if (stu[j].score < stu[j + 1].score) {
                        temp = stu[j];
                        stu[j] = stu[j + 1];
                        stu[j + 1] = temp;
                    }
                }
            }
        } else if (c == 1) {
            //升序
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n-i; j++) {
                    if (stu[j].score > stu[j + 1].score) {
                        temp = stu[j];
                        stu[j] = stu[j + 1];
                        stu[j + 1] = temp;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            printf("%s %d\n", stu[i].name, stu[i].score);
        }
    }
    return 0;
}