import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int number = Integer.parseInt(sc.nextLine());
String sort = sc.nextLine();
Student[] students = new Student[number];
for (int i = 0; i < number; i++) {
String line = sc.nextLine();
String[] split = line.split(" ");
students[i] = new Student(split[0], Integer.parseInt(split[1]));
}
Arrays.sort(students, (o1, o2) -> {
if (sort.equals("0")) {
return o2.score - o1.score;
} else {
return o1.score - o2.score;
}
}
);
for (Student student : students) {
System.out.println(student.name + " " + student.score);
}
}
}
public static class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
}
}