#include <stdio.h>

int main() {
    int n, order;
    while (scanf("%d\n%d", &n, &order) != EOF) {
        char name[201][20];
        int score[201];
        int max = 0, min = 100;
        for(int i = 0; i < n; i++) {
            scanf("%s %d", name[i], &score[i]);
            if(score[i] > max) max = score[i];
            if(score[i] < min) min = score[i];
        }
        if(order == 0) {
            for(int i = max; i >= min; i--) {
                for(int j = 0; j < n; j++) {
                    if(score[j] == i) {
                        printf("%s %d\n", name[j], score[j]);
                    }
                }
            }
        } else {
            for(int i = min; i <= max; i++) {
                for(int j = 0; j < n; j++) {
                    if(score[j] == i) {
                        printf("%s %d\n", name[j], score[j]);
                    }
                }
            }
        }
       
    }
    return 0;
}