ExpertLists & Dictionaries50 points

Build the league table with head-to-head tie-breaks

A football federation in Buenos Aires must publish the final table of a small league. Several teams often finish on the same points, and the rulebook says how to separate them. Your program replaces the spreadsheet the federation used to rely on.

Task: line 1 is the number of matches m. Then m lines Home Away homeGoals awayGoals (team names have no spaces; two teams can meet any number of times). A win gives 3 points, a draw 1, a loss 0. Rank the teams with these keys, in order: (1) points, higher first; (2) head-to-head points: among all teams that have the same total points, count only the points each of them earned in matches played between members of that group; higher first (a team alone on its points has 0; apply this once, no recursion); (3) goal difference, higher first; (4) goals scored, higher first; (5) team name, in plain ascending string order. Print one line per team in rank order: pos team P W D L GF:GA GD Pts where P is matches played, GD is shown with an explicit sign (like +3, -2, +0), and pos counts 1, 2, 3, ... (teams never share a position).

Sample tests

Input

12
Boca River 2 1
River Boca 0 0
Boca Lanus 1 0
Lanus Boca 2 2
River Lanus 3 0
Lanus River 1 1
Boca Racing 0 3
Racing Boca 1 1
River Racing 2 2
Racing River 0 1
Lanus Racing 2 0
Racing Lanus 1 4

Expected output

1 Boca 6 2 3 1 6:7 -1 9
2 River 6 2 3 1 8:5 +3 9
3 Lanus 6 2 2 2 9:8 +1 8
4 Racing 6 1 2 3 7:10 -3 5

Input

3
A B 1 0
B C 1 0
C A 1 0

Expected output

1 A 2 1 0 1 1:1 +0 3
2 B 2 1 0 1 1:1 +0 3
3 C 2 1 0 1 1:1 +0 3

Your answer is also checked against 3 hidden tests.