# include<iostream>
# include<algorithm>
using namespace std;

struct Student{
    string name;
    int score;
};

bool Ascending(Student stu1,Student stu2){
        return stu1.score<stu2.score;
};
bool Descending(Student stu1,Student stu2){
    return stu1.score>stu2.score;
};

int main(){

    int n;
    int method;
    Student student[100];
    //cout<<"请输入学生人数";
    cin>>n;
    //cout<<"请输入排序方法0(降序)或者1(升序)";
    cin>>method;
    for(int i=0;i<n;i++){
        cin>>student[i].name>>student[i].score;
    };     
    if(method==1){
        sort(student,student+n,Ascending);
    }
    else if(method==0){
        sort(student,student+n,Descending);
    }
    else cout<<"排序方法输入错误"
    ;
    for(int i=0;i<n;i++){
        cout<<student[i].name<<" "<<student[i].score<<endl;
    }
}