#include <stdio.h>

typedef struct {
    int student_id;
    int math;
    int english;
    int chinese;
    int total;
} Student;

int main() {
    int n;
    int i,j;
    scanf("%d", &n);
    
    if (n >= 10) {
        printf("Please input 1-9.\n");
        return 0;
    }

    Student students[10];
    for (i = 0; i < n; i++) {
        scanf("%d %d %d %d", &students[i].student_id, &students[i].math, &students[i].english, &students[i].chinese);
        students[i].total = students[i].math + students[i].english + students[i].chinese;
    }

    for (i = 0; i < n; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (students[j].total < students[j + 1].total || (students[j].total == students[j + 1].total && students[j].student_id > students[j + 1].student_id)) {
                Student temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }

    for (i = 0; i < n; i++) {
        printf("%4d%10d%5d%5d%5d\n", i + 1, students[i].student_id, students[i].math, students[i].english, students[i].chinese);
    }

    return 0;
}
