#include <stdio.h>
#include <string.h>

typedef struct student {
    int score;
    char name[200];
} student;

void bubbleSort(int *a, int n, int kind) {
    int temp;
    if (kind == 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] < a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
    if (kind == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}

int main() {
    int n, kind;
    while (scanf("%d %d", &n, &kind) != EOF) {
        int a[200];
        student stu[200];
        for (int i = 0; i < n; i++) {
            scanf("%s %d", stu[i].name, &stu[i].score);
            a[i] = stu[i].score;
        }

        bubbleSort(a, n, kind);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i] == stu[j].score) {
                    printf("%s %d\n", stu[j].name, stu[j].score);
                    stu[j].score = -1; // 标记该学生已经输出过
                    break;
                }
            }
        }
    }

    return 0;
}