#include <cstdio>
#include <algorithm>
using namespace std;
struct Student{
    char name[50];
    int grade;
    int seq;
};
bool comp1(Student lsh,Student rsh){
    if (lsh.grade<rsh.grade){
        return true;
    } else if(lsh.grade==rsh.grade && lsh.seq < rsh.seq){
        return true;
    }
    else{
        return false;
    }
}
bool comp0(Student lsh,Student rsh){
    if (lsh.grade>rsh.grade){
        return true;
    }  else if(lsh.grade==rsh.grade && lsh.seq < rsh.seq){
        return true;
    }else{
        return false;
    }
}
int main(){
    int N;
    int order;
    Student arr[100];
    while (scanf("%d%d",&N,&order)!=EOF){
        int seq=0;
        for (int i = 0; i < N; ++i) {
            scanf("%s%d",arr[i].name,&arr[i].grade);//name是一个字符,字符自动是地址。
            arr[i].seq=seq;
            ++seq;
        }
        if (order==0){
            sort(arr,arr+N, comp0);
        } else if(order==1){
            sort(arr,arr+N, comp1);
        } else{
            return -1;
        }
        for (int i = 0; i < N; ++i) {
            printf("%s %d\n",arr[i].name,arr[i].grade);
        }
    }

}