#include <stdio.h>
#define MAXSIZE 30
typedef struct test {
    char a[MAXSIZE];
    int number;
} Grade;
int main() {
    int n;
    while ((scanf("%d", &n)) != EOF) {
        int flag;
        scanf("%d", &flag);
        Grade G[n];
        int i = 0;
        int count = n;
        int m = n;
        int p = n;
        while (n--) {
            scanf("%s %d", G[i].a, &G[i].number);
            i++;
        }
        if (flag == 1) {
            while (m--) {
                for (int k = 0; k < m; k++) {
                    if (G[k].number > G[k + 1].number) {
                        Grade Gn = G[k + 1];
                        G[k + 1] = G[k];
                        G[k] = Gn;
                    }
                }
            }
        } else if (flag == 0) {
            while (p--) {
                for (int q = 0; q < p; q++) {
                    if (G[q].number <G[q + 1].number) {
                        Grade Gm = G[q + 1];
                        G[q + 1] = G[q];
                        G[q] = Gm;
                    }
                }
            }
        } else {
            printf("flag值输入错误\n");
            return 0;
        }
        for (int j = 0; j < count; j++) {
            printf("%s %d\n", G[j].a, G[j].number);
        }
    }
    return 0;
}