//
// Created by gs on 2026/3/6.
//

// HJ68 成绩排序


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

typedef struct {
    char name[21];
    int score;
} STUDENT;

void student_cpy(STUDENT* dst, STUDENT* src) {
    strcpy(dst->name, src->name);
    dst->score = src->score;
}

int main() {
    int n, op, i, j;
    STUDENT student[201];
    STUDENT min, temp;
    scanf("%d", &n);
    scanf("%d", &op);
    i = 0;
    while (scanf("%s %d", student[i].name, &student[i].score) != EOF) {
        i++;
        if (i == n)
            break;
    }

    if (op == 1) {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n - i - 1; j++) {
                if (student[j].score > student[j + 1].score) {
                    student_cpy(&temp, &student[j]);
                    student_cpy(&student[j], &student[j + 1]);
                    student_cpy(&student[j + 1], &temp);
                }
            }
        }
    } else {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n - i - 1; j++) {
                if (student[j].score < student[j + 1].score) {
                    student_cpy(&temp, &student[j]);
                    student_cpy(&student[j], &student[j + 1]);
                    student_cpy(&student[j + 1], &temp);
                }
            }
        }
    }

    for (i = 0; i < n; i++) {
        printf("%s %d\n", student[i].name, student[i].score);
    }



    return 0;
}