import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyStore.Entry;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

// TreeMap实现
public class Main {

public static void main(String[] args) throws NumberFormatException, IOException  {

    // 注意 hasNext 和 hasNextLine 的区别
      BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
      String str="";
      while((str=br.readLine())!=null){
        int groupNum=Integer.valueOf(str );
        String mode=br.readLine().trim();
        String [] a=new String [groupNum];
        for(int i=0;i<groupNum;i++) {
            a[i]=br.readLine().trim();
        }
        TreeMap<StudentInfo,String> map=new TreeMap<>();
        for(int i=0;i<groupNum;i++) {
            String temp=a[i];
            String [] tempArr=temp.split(" ");
            StudentInfo tempStudent=
                    new StudentInfo(tempArr[0], Integer.valueOf(tempArr[1]));
            map.put( tempStudent,temp );
        } 
        print(mode, map);
    }
}
private static void print(String mode, TreeMap<Main.StudentInfo, String> m) {
    // TODO Auto-generated method stub

    if("0".equals(mode)) {
        for(java.util.Map.Entry<Main.StudentInfo, String> entry :m.entrySet()) {
              System.out.println( entry.getValue()); 
        }
    }
    else {
        Map <Main.StudentInfoDes, String> map=new TreeMap<Main.StudentInfoDes, String>();
        map= des(m);
        for(java.util.Map.Entry<Main.StudentInfoDes, String> entry :map.entrySet()) {
             System.out.println( entry.getValue()); 
       }
    }

}
private static Map<Main.StudentInfoDes, String> des(TreeMap<Main.StudentInfo, String> m) {
   Map <Main.StudentInfoDes, String> map=new TreeMap<Main.StudentInfoDes, String>();

   for(java.util.Map.Entry<Main.StudentInfo, String> entry :m.entrySet()) {
       StudentInfoDes s_des=new StudentInfoDes(entry.getKey().name, entry.getKey().grade);
        map.put(s_des, entry.getValue());
   }
    return map;
}
public static class StudentInfo implements Comparable<StudentInfo>{
    private String name;
    private Integer grade;
    StudentInfo(String name,Integer grade){
        this.name=name;
        this.grade=grade;
    }
    @Override
    public int compareTo(Main.StudentInfo o) {
        if( o.grade-this.grade==0  ) {
            return 1;
        }
        else
            return  o.grade-this.grade ; 
    }

    @Override
    public int hashCode() {

        return name.hashCode();
    }


}
public static class StudentInfoDes implements Comparable<StudentInfoDes>{
    private String name;
    private Integer grade;
    StudentInfoDes(String name,Integer grade){
        this.name=name;
        this.grade=grade;
    }
    @Override
    public int compareTo(Main.StudentInfoDes o) {
        if( o.grade-this.grade==0  ) {
            return  1;
        }
        else
            return  -o.grade+this.grade ; 
    }

    @Override
    public int hashCode() {

        return name.hashCode();
    }


}

}