#include<bits/stdc++.h>
using namespace std;

struct student{
    int num;
    string name;
    int grade;
};

bool cmp0(student x,student y)
{
    if(x.grade==y.grade)
    {
        return x.num<y.num;
    }
    else
    {
        return x.grade>y.grade;
    }
}

bool cmp1(student x,student y)
{
    if(x.grade==y.grade)
    {
        return x.num<y.num;
    }
    else
    {
        return x.grade<y.grade;
    }
}

int main()
{
    int n;
    int way;
    vector<student> a;
    student t;
//    scanf("%d",&n);
//    scanf("%d",&way);
    //vector<student>::iterator p;
    //vector<student>::iterator ptr;
    //p=a.begin();
    
    while(scanf("%d%d",&n,&way)!=EOF) 
    {
        //ptr=p;
        //完成输入 
        for(int i=0;i<n;i++)
        {
            cin>>t.name;
            scanf("%d",&t.grade);
            //cout<<"lllllllllllllllllllll";
            t.num=i;
            //cout<<"kkkkkkkkkkkkkkkk";
            a.push_back(t);
            //p++;
        }
        //降序 
        if(way==0)
        {
            sort(a.end()-n,a.end(),cmp0);
        }
        
        //升序
        if(way==1)
        {
            sort(a.end()-n,a.end(),cmp1);    
        
        
    }    
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i].name<<" "<<a[i].grade<<endl; 
    }
    
}