#include<iostream>
//#incldue <stdlib.h>
#include <algorithm> //调用sort函数必备;
#include<cstring>
using namespace std;
class wujiang{
public:
wujiang(){
}
wujiang(char *p,int ti ,int wu1,int zhi){
tili=ti;
wuli=wu1;
zhili=zhi;
strncpy(wu, p , sizeof(wu));
}
char wu[20];
int tili;
int wuli;
int zhili;
protected:
};
bool compare_function1(const wujiang &a,const wujiang &b)
{
return a.tili > b.tili;
}
bool compare_function2(const wujiang &a,const wujiang &b)
{
return a.wuli > b.wuli;
}
bool compare_function3(const wujiang &a,const wujiang &b)
{
return a.zhili > b.zhili;
}
int main(){
int n=20;
int num=0;
wujiang p [n];
for(int u=0;;u++){
char cii[20];
cin>>cii;
if(cii[0]=='#')break;
num++;
char *ci=cii;
int tili;cin>>tili;
int wuli;cin>>wuli;
int zhili;cin>>zhili;
p[u]=wujiang(ci,tili,wuli,zhili);
}
int cint;
cin>>cint;
switch(cint){
case 1: sort(p,p+num,compare_function1);break;
case 2: sort(p,p+num,compare_function2);break;
case 3: sort(p,p+num,compare_function3);break;
}
for(int u=0;u<num;u++){
cout<<p[u].wu;
cout<<" ";
cout<<p[u].tili;
cout<<" ";
cout<<p[u].wuli;
cout<<" ";
cout<<p[u].zhili;
cout<<" "<<endl;
}
return 0;
}