#include<bits/stdc++.h>
using namespace std;
struct jilu{
    int x;
    int y;
    float a;
    float b;
    float c;
}s[100005];
struct paiming{
    int xuehao;
    float zongfen;
}f[100005];
int cmp(int x,int y){
    if(f[x].zongfen>f[y].zongfen){
        if(f[x].xuehao>f[y].xuehao)return x>y;
        else return x<y;
    }else{
        return x<y;
    }
    return x>y;
    
}
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>s[i].x>>s[i].y>>s[i].a>>s[i].b>>s[i].c;
        f[i].xuehao=s[i].y;
        f[i].zongfen=(4*s[i].a+4*s[i].b+2*s[i].c)/10;
    }
    sort(f+1,f+m+1,cmp);
    /*for(int i=1;i<=m;i++){
        for(int j=1;j<=i;j++){
            if(f[i].zongfen>f[j].zongfen){
                if(f[i].xuehao>f[j].xuehao){
                    swap(f[i],f[j]);
                }else{
                    swap(f[j],f[i]);
                }
            }
        }
    }*/
    for(int i=1;i<=m;i++){
        cout<<f[i].xuehao<<" "<<f[i].zongfen<<endl;
    }
    return 0;
}