#include <iostream>
#include <fstream>
using namespace std;
ifstream f("purtare.in");
ofstream g("purtare.out");
struct date{int nmatr, nt, nam, np;}elev[40];
int nmatr, nt, nam, np, i, n, j;
int main()
{
f >> n;
for (i=0; i<n; ++i)
{
f >> elev[i].nmatr >> elev[i].nt >> elev[i].nam >> elev[i].np;
if (elev[i].nt>50) elev[i].np=4;
else elev[i].np=10-(elev[i].nt-elev[i].nam)/10;
}
for (i=0; i<n-1; ++i)
{
for (j=i+1; j<n; ++j)
if (elev[j].np>elev[i].np) swap(elev[i], elev[j]);
}
for (i=0; i<n; ++i)
g << elev[i].nmatr<<" "<<elev[i].nt<<" "<<elev[i].nam<<" "<<elev[i].np<<"\n";
}