#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
const int N = 100;

struct stu{
    int id;
    string name;
    int score;
};
int type;
bool cmp(stu a,stu b){
    if(type == 0){
        if(a.score == b.score) return a.id < b.id;
        else return a.score > b.score;
    }else{
        if(a.score == b.score) return a.id < b.id;
        else return a.score < b.score;
    }
}

int main(){
    int n;
    while(cin >> n){
        cin >> type;
        vector<stu> a(n);
        for(int i = 0; i < n ;i++){
            cin >> a[i].name >> a[i].score;
            a[i].id = i;
        }

        stable_sort(a.begin(),a.end(),cmp);

        for(int i = 0 ; i<n;i++){
            cout << a[i].name << " " << a[i].score << endl;
        }

    }
}

没什么好说的,模板题