#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
//主要在于维持排序稳定
typedef struct Student
{
string name;
int number;
double score;
};
bool cmp1(const Student & s1,const Student & s2)//降序,排序稳定
{
if(s1.score == s2.score)//分数相同,维持两元素的相对位置不变
return s1.number < s2.number;
else
return s1.score > s2.score;
}
bool cmp2(const Student & s1,const Student & s2)//升序
{
if(s1.score == s2.score)
return s1.number < s2.number;
else
return s1.score < s2.score;
}
int main(void)
{
int m;
vector<Student> arr;
while(cin >> m)
{
arr.clear();
int c;
cin >> c;
int i = 0;//记录学生在原case中的位置
while(m--)//输入学生信息
{
Student s;
cin >> s.name >> s.score;
s.number = i;
i++;
arr.push_back(s);
}
if(c == 0)//选择排序方法
sort(arr.begin(),arr.end(),cmp1);
else
sort(arr.begin(), arr.end(),cmp2);
for(vector<Student>::iterator it = arr.begin();it != arr.end();it++)
cout << it->name << ' ' << it->score << endl;
}
return 0;
}