import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int n = Integer.parseInt(sc.nextLine());
int u = Integer.parseInt(sc.nextLine());
List<Stu> list = new ArrayList<Stu>();
for (int i =0; i< n; i++) {
String[] arr = sc.nextLine().split(" ");
list.add(new Stu(arr[0], Integer.parseInt(arr[1])));
}
if (u == 0) {
Collections.sort(list, new Comparator<Stu>() {
@Override
public int compare(Stu s1, Stu s2) {
return s2.score - s1.score;
}
});
} else if (u == 1) {
Collections.sort(list, new Comparator<Stu>() {
@Override
public int compare(Stu s1, Stu s2) {
return s1.score - s2.score;
}
});
}
for(int i =0; i< n; i++) {
Stu s = list.get(i);
System.out.println(s.name + " " + s.score);
}
}
}
}
class Stu {
String name;
int score;
public Stu(String name, int score) {
this.name = name;
this.score = score;
}
}