#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<string,int> st1,pair<string,int> st2){
    if(st1.second==st2.second){
        return false;
    }else return st1.second<st2.second;
}

bool cmpr(pair<string,int> st1,pair<string,int> st2){
    if(st1.second==st2.second){
        return false;
    }else return st1.second>st2.second;
}
int main() {
    int  n,s;
    cin>>n>>s;
    string name;
    int score;
    vector<pair<string,int> > vec;
    for(int i=0;i<n;i++){
        cin>>name>>score;
        vec.push_back(pair(name,score));
    }
    vector<pair<string,int> > tmp(vec);

    if(s==1)stable_sort(vec.begin(),vec.end(),cmp);
    if(s==0)stable_sort(vec.begin(),vec.end(),cmpr);
    for(int i=0;i<vec.size();i++){
        cout<<vec[i].first<<" "<<vec[i].second<<endl;
    }
}
// 64 位输出请用 printf("%lld")