#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student {
string name;
int score;
//标记顺序
int flag;
};
/**
* 降序排序
* @param x
* @param y
* @param flag
* @return
*/
bool compareDescend(Student x, Student y) {
//按成绩从高到低排序
if (x.score == y.score) {
return x.flag < y.flag;
} else {
return x.score > y.score;
}
}
/**
* 升序排序
* @param x
* @param y
* @return
*/
bool compareAscend(Student x, Student y) {
//按成绩从低到高排序
if (x.score == y.score) {
return x.flag < y.flag;
} else {
return x.score < y.score;
}
}
void print(Student student[], int n) {
for (int i = 0; i < n; ++i) {
cout << student[i].name << " " << student[i].score << endl;
}
}
/**
* 成绩排序--清华大学
* @return
*/
int main() {
int m;
/*
* 排序标记,0--降序;1--升序
*/
int flag;
while (cin >> m >> flag) {
Student *student = new Student[m];
for (int i = 0; i < m; ++i) {
cin >> student[i].name >> student[i].score;
student[i].flag = i;
}
if (flag == 0) {
//降序
sort(student, student + m, compareDescend);
} else {
//升序
sort(student, student + m, compareAscend);
}
print(student, m);
}
return 0;
}