
import std.stdio, std.algorithm;

void main() {
  bool first = true;
  for(int times; readf("%d\n", &times), times != 0;) {
    if (!first) {writeln;}
    else first = false;
    
    int[string] points;
    string[] countries;
    foreach(t; 0..times) {
      int win, lose, draw;
      string name;
      readf("%s %d %d %d\n", &name, &win, &lose, &draw);
      points[name] = win * 3 + draw * 1 + lose * 0;
      countries ~= name;
    }
    string[] names = points.keys;
    sort!((x, y) => points[x] > points[y])(names);
    foreach(n; names) {
      writeln(n, " ", points[n]);
    }
  }
}