Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.

The UEFA Champions League is the most prestigious annual sports competition in the world. In the group stage of this competition, European football clubs are divided into 8 groups; there are four teams in each group. The teams in each group are ranked based on the matches they play against each other, according to the following rules:
Based on the results of matches, teams are awarded *points*. Each football match is played between a *home team* and an *away team*. If one of the teams scores more goals than the other, this team gains $3$ points and the other team gains $0$ points. In case of a tie (if both teams score the same number of goals), each of those teams gains $1$ point.
The *goal difference* of a team is the number of goals it scored minus the number of goals it received, regardless if it scored/received them as the home team or as the away team.
Between any two teams, the team with more points is ranked higher.
If they have the same number of points (in case of a tie), the team with higher goal difference is ranked higher.

Each team plays two matches against every other team in its group ― one match as the home team and one match as the away team. You are given the number of goals scored by each team for all twelve matches in one group. Determine the leaders of this group ― the first and second top-ranked team. It is guaranteed that there are no ties for either of these places (for the given results of the matches).

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
For each test case, $12$ lines follow. Each of these lines describes the result of one match in the format HomeTeamName HomeTeamGoals vs. AwayTeamGoals AwayTeamName, where HomeTeamName and AwayTeamName are strings and HomeTeamGoals and AwayTeamGoals are integers denoting the number of goals scored by the respective teams in this match.

------  Output ------
For each scenario, print a single line containing two space-separated strings ― the name of the top-ranked team and the name of the second top-ranked team.

------  Constraints ------
$1 ≤ T ≤ 50$
the length of the name of each team does not exceed $10$
the name of each team contains only lowercase English letters
$0 ≤$ number of goals scored by each team $≤ 100$

----- Sample Input 1 ------ 
2

manutd 8 vs. 2 arsenal

lyon 1 vs. 2 manutd

fcbarca 0 vs. 0 lyon

fcbarca 5 vs. 1 arsenal

manutd 3 vs. 1 fcbarca

arsenal 6 vs. 0 lyon

arsenal 0 vs. 0 manutd

manutd 4 vs. 2 lyon

arsenal 2 vs. 2 fcbarca

lyon 0 vs. 3 fcbarca

lyon 1 vs. 0 arsenal

fcbarca 0 vs. 1 manutd

a 3 vs. 0 b

a 0 vs. 0 c

a 0 vs. 0 d

b 0 vs. 0 a

b 4 vs. 0 c

b 0 vs. 0 d

c 0 vs. 0 a

c 0 vs. 0 b

c 1 vs. 0 d

d 3 vs. 0 a

d 0 vs. 0 b

d 0 vs. 0 c
----- Sample Output 1 ------ 
manutd fcbarca

d b
----- explanation 1 ------ 
Example case 1: The total number of points and goal difference for each team is as follows:
- manutd: $16$ points, goal difference $12$
- fcbarca: $8$ points, goal difference $4$
- manutd: $5$ points, goal difference $-5$
- lyon: $4$ points, goal difference $-11$

Example case 2: The total number of points and goal difference for each team is as follows:
- d: $7$ points, goal difference $2$
- b: $7$ points, goal difference $1$
- a: $7$ points, goal difference $0$
- c: $7$ points, goal difference $-3$

Note that in this test case, all teams have the same number of points, but teams with higher goal difference are ranked higher.

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Sorting

Implementation

Complete search

Data structures

Ad-hoc
Skill types: Sorting

Data structures

Complete search

Starter code:

Solutions:
T = int(input())
for _ in range(T):
	d = {}
	initial_score = 0
	initial_goal = 0
	for i in range(12):
		scoreline = str(input())
		score = scoreline.split()
		if score[0] not in list(d.keys()):
			d[score[0]] = [initial_score, initial_goal]
		if score[4] not in list(d.keys()):
			d[score[4]] = [initial_score, initial_goal]
		if int(score[1]) > int(score[3]):
			d[score[0]][0] += 3
			d[score[0]][1] = d[score[0]][1] + (int(score[1]) - int(score[3]))
			d[score[4]][1] = d[score[4]][1] + (int(score[3]) - int(score[1]))
		elif int(score[3]) > int(score[1]):
			d[score[4]][0] += 3
			d[score[4]][1] = d[score[4]][1] + (int(score[3]) - int(score[1]))
			d[score[0]][1] = d[score[0]][1] + (int(score[1]) - int(score[3]))
		else:
			d[score[0]][0] += 1
			d[score[4]][0] += 1
	table = list(dict(sorted(d.items(), key=lambda item: item[1])).keys())
	print(table[-1], table[-2])


for _ in range(int(input())):
	teams = {}
	arr = [[0, 0, ''] for _ in range(4)]
	z = 0
	for i in range(12):
		(team1, team1score, x, team2score, team2) = list(input().split())
		team1score = int(team1score)
		team2score = int(team2score)
		if team1 not in teams:
			teams[team1] = z
			arr[teams[team1]][2] = team1
			z += 1
		if team2 not in teams:
			teams[team2] = z
			arr[teams[team2]][2] = team2
			z += 1
		if team1score > team2score:
			arr[teams[team1]][0] += 3
		elif team2score > team1score:
			arr[teams[team2]][0] += 3
		else:
			arr[teams[team2]][0] += 1
			arr[teams[team1]][0] += 1
		arr[teams[team1]][1] += team1score - team2score
		arr[teams[team2]][1] += team2score - team1score
	arr.sort(reverse=True)
	print(arr[0][2], arr[1][2])


n = int(input())
for i in range(n):
	d = {}
	gd = {}
	for i in range(12):
		ar = list(map(str, input().split(' ')))
		t1 = ar[0]
		t2 = ar[4]
		v1 = int(ar[1])
		v2 = int(ar[3])
		if v1 > v2:
			if t1 in d:
				d[t1] += 3
			else:
				d[t1] = 3
			if t1 in gd:
				gd[t1] += v1 - v2
			else:
				gd[t1] = v1 - v2
			if t2 in gd:
				gd[t2] += v2 - v1
			else:
				gd[t2] = v2 - v1
		elif v2 > v1:
			if t2 in d:
				d[t2] += 3
			else:
				d[t2] = 3
			if t2 in gd:
				gd[t2] += v2 - v1
			else:
				gd[t2] = v2 - v1
			if t1 in gd:
				gd[t1] += v1 - v2
			else:
				gd[t1] = v1 - v2
		else:
			if t1 in d:
				d[t1] += 1
			else:
				d[t1] = 1
			if t2 in d:
				d[t2] += 1
			else:
				d[t2] = 1
	l = []
	for i in d.keys():
		l.append([d[i], gd[i], i])
	l.sort(reverse=True)
	print(l[0][2], l[1][2])


from collections import *
for _ in range(int(input())):
	r = defaultdict(lambda : [0, 0, 0])
	for _ in range(12):
		(ht, hg, _, ag, at) = input().split()
		hg = int(hg)
		ag = int(ag)
		r[ht][1] += hg
		r[ht][2] += ag
		r[at][1] += ag
		r[at][2] += hg
		if hg == ag:
			r[ht][0] += 1
			r[at][0] += 1
		elif hg > ag:
			r[ht][0] += 3
		else:
			r[at][0] += 3
	print(*[k for (_, _, k) in sorted([(-a, c - b, k) for (k, [a, b, c]) in r.items()])[:2]])


from math import *
import sys

def input():
	return sys.stdin.readline().replace('\n', '').strip()
sys.setrecursionlimit(10 ** 9)
for _ in range(int(input())):
	d = {}
	d1 = {}
	for i in range(12):
		s = input().split()
		n1 = s[0]
		n2 = s[-1]
		n1g = int(s[1])
		n2g = int(s[3])
		if n1 not in d:
			d[n1] = (0, 0)
		if n2 not in d:
			d[n2] = (0, 0)
		(a, b) = d[n1]
		if n1g == n2g:
			a += 1
		elif n1g > n2g:
			a += 3
		else:
			a += 0
		b += n1g - n2g
		d[n1] = (a, b)
		(a, b) = d[n2]
		if n1g == n2g:
			a += 1
		elif n2g > n1g:
			a += 3
		else:
			a += 0
		b += n2g - n1g
		d[n2] = (a, b)
	l1 = []
	for (x, (y, z)) in d.items():
		l1.append((y, z, x))
	l1.sort()
	print(l1[-1][-1], l1[-2][-1])


t = int(input())
for _ in range(t):
	dct = {}
	for i in range(12):
		lst = list(input().split())
		(goal_home_team, goal_away_team) = (int(lst[1]), int(lst[-2]))
		(home_team, away_team) = (lst[0], lst[-1])
		(points_home_team, points_away_team) = (0, 0)
		if goal_home_team > goal_away_team:
			points_home_team = 3
		elif goal_home_team < goal_away_team:
			points_away_team = 3
		else:
			(points_home_team, points_away_team) = (1, 1)
		if home_team not in dct:
			dct[home_team] = [points_home_team, goal_home_team - goal_away_team]
		else:
			team_data = dct[home_team]
			team_data[0] += points_home_team
			team_data[1] += goal_home_team - goal_away_team
		if away_team not in dct:
			dct[away_team] = [points_away_team, goal_away_team - goal_home_team]
		else:
			team_data = dct[away_team]
			team_data[0] += points_away_team
			team_data[1] += goal_away_team - goal_home_team
	final_lst = [dct[ele] + [ele] for ele in dct]
	final_lst.sort(reverse=True)
	print(final_lst[0][-1], final_lst[1][-1])


T = int(input())
for _ in range(T):
	D = {}
	for i in range(12):
		(a, b, e, d, c) = map(str, input().split())
		if a in D:
			if int(b) > int(d):
				D[a][0] += 3
			elif int(b) == int(d):
				D[a][0] += 1
			D[a][1] += int(b) - int(d)
		elif int(b) > int(d):
			D[a] = [3, int(b) - int(d)]
		elif int(b) == int(d):
			D[a] = [1, int(b) - int(d)]
		else:
			D[a] = [0, int(b) - int(d)]
		if c in D:
			if int(d) > int(b):
				D[c][0] += 3
			elif int(b) == int(d):
				D[c][0] += 1
			D[c][1] += int(d) - int(b)
		elif int(b) < int(d):
			D[c] = [3, int(d) - int(b)]
		elif int(b) == int(d):
			D[c] = [1, int(b) - int(d)]
		else:
			D[c] = [0, int(d) - int(b)]
	M = []
	for m in D:
		D[m].append(m)
		M.append(D[m])
	M.sort(reverse=True)
	print(M[0][2], M[1][2])


t = int(input())
while t > 0:
	tot = []
	x = 12
	groups = []
	score = [0 for i in range(4)]
	gd = [0 for i in range(4)]
	while x > 0:
		a = list(map(str, input().split()))
		t1 = a[0]
		s1 = a[1]
		s1 = int(s1)
		s2 = a[3]
		s2 = int(s2)
		t2 = a[4]
		if t1 not in groups:
			groups.append(t1)
		if t2 not in groups:
			groups.append(t2)
		if s1 > s2:
			score[groups.index(t1)] += 3
		elif s2 > s1:
			score[groups.index(t2)] += 3
		else:
			score[groups.index(t1)] += 1
			score[groups.index(t2)] += 1
		gd[groups.index(t1)] += s1 - s2
		gd[groups.index(t2)] += s2 - s1
		x = x - 1
	for i in range(4):
		tot.append([score[i], gd[i], groups[i]])
	tot.sort()
	out = []
	out.append(tot[-1][-1])
	out.append(tot[-2][-1])
	print(*out)
	t = t - 1


T = int(input())
for i in range(T):
	d = {}
	for j in range(12):
		(s1, t1, v, t2, s2) = map(str, input().split(' '))
		if s1 not in d:
			d[s1] = [0, 0]
		if s2 not in d:
			d[s2] = [0, 0]
		t1 = int(t1)
		t2 = int(t2)
		if t1 > t2:
			d[s1][0] += 3
			d[s1][1] += t1 - t2
			d[s2][1] += t2 - t1
		elif t1 < t2:
			d[s2][0] += 3
			d[s1][1] += t1 - t2
			d[s2][1] += t2 - t1
		else:
			d[s1][0] += 1
			d[s2][0] += 1
	l = []
	for i in d.keys():
		l.append([d[i][0], d[i][1], i])
	l.sort(reverse=True)
	print(l[0][2], l[1][2])


t = int(input())
for _ in range(t):
	di = {}
	for i in range(12):
		(t1, s1, v, s2, t2) = map(str, input().split())
		if t1 not in di:
			di[t1] = [0, 0]
		if t2 not in di:
			di[t2] = [0, 0]
		s1 = int(s1)
		s2 = int(s2)
		if s1 > s2:
			di[t1][0] += 3
			di[t1][1] += s1 - s2
			di[t2][1] += s2 - s1
		if s2 > s1:
			di[t2][0] += 3
			di[t2][1] += s2 - s1
			di[t1][1] += s1 - s2
		if s1 == s2:
			di[t1][0] += 1
			di[t2][0] += 1
	fa = []
	for i in di.keys():
		fa.append([di[i][0], di[i][1], i])
	fa.sort(reverse=True)
	print(fa[0][2], fa[1][2])


n = int(input())
for j in range(n):
	arr = []
	s = []
	count = 0
	d = {}
	c = {}
	j = []
	for i in range(12):
		arr = list(input().split())
		s.append(arr)
		a = int(s[i][1])
		b = int(s[i][3])
		q = []
		u = {}
		s[i][1] = a
		s[i][3] = b
		if s[i][0] not in d:
			d[s[i][0]] = 0
			c[s[i][0]] = 0
		if s[i][4] not in d:
			d[s[i][4]] = 0
			c[s[i][4]] = 0
		if s[i][1] > s[i][3]:
			d[s[i][0]] += 3
			c[s[i][0]] += s[i][1] - s[i][3]
			c[s[i][4]] += s[i][3] - s[i][1]
		elif s[i][3] > s[i][1]:
			d[s[i][4]] += 3
			c[s[i][4]] += s[i][3] - s[i][1]
			c[s[i][0]] += s[i][1] - s[i][3]
		else:
			d[s[i][0]] += 1
			d[s[i][4]] += 1
	u = list(d.keys())
	final = []
	for i in range(4):
		final.append([d[u[i]], c[u[i]], u[i]])
	final.sort(reverse=True)
	print(*[final[0][2], final[1][2]])


t = int(input())
while t:
	t -= 1
	(pnts, gldif) = ({}, {})
	for i in range(12):
		ls = list(input().split())
		(nm1, gl1, vs, gl2, nm2) = (ls[0], int(ls[1]), ls[2], int(ls[3]), ls[4])
		if nm1 not in pnts:
			if gl1 > gl2:
				pnts[nm1] = 3
			elif gl2 > gl1:
				pnts[nm1] = 0
			else:
				pnts[nm1] = 1
		elif gl1 > gl2:
			pnts[nm1] += 3
		elif gl2 > gl1:
			pnts[nm1] += 0
		else:
			pnts[nm1] += 1
		if nm2 not in pnts:
			if gl2 > gl1:
				pnts[nm2] = 3
			elif gl1 > gl2:
				pnts[nm2] = 0
			else:
				pnts[nm2] = 1
		elif gl2 > gl1:
			pnts[nm2] += 3
		elif gl1 > gl2:
			pnts[nm2] += 0
		else:
			pnts[nm2] += 1
		if nm1 not in gldif:
			gldif[nm1] = gl1 - gl2
		else:
			gldif[nm1] += gl1 - gl2
		if nm2 not in gldif:
			gldif[nm2] = gl2 - gl1
		else:
			gldif[nm2] += gl2 - gl1
	fin_ls = []
	pkeys = list(pnts.keys())
	pval = list(pnts.values())
	for i in range(len(pnts)):
		fin_ls.append([pnts[pkeys[i]], gldif[pkeys[i]], pkeys[i]])
	fin_ls.sort(reverse=True)
	print(*[fin_ls[0][2], fin_ls[1][2]])


t = int(input())
for k in range(t):
	names = []
	scores = [0] * 4
	diff = [0] * 4
	for j in range(12):
		(teamname1, teamgoal1, k, teamgoal2, teamname2) = input().split()
		teamgoal1 = int(teamgoal1)
		teamgoal2 = int(teamgoal2)
		if teamname1 not in names:
			names.append(teamname1)
		if teamname2 not in names:
			names.append(teamname2)
		if teamgoal1 > teamgoal2:
			scores[names.index(teamname1)] += 3
		elif teamgoal2 > teamgoal1:
			scores[names.index(teamname2)] += 3
		else:
			scores[names.index(teamname1)] += 1
			scores[names.index(teamname2)] += 1
		diff[names.index(teamname1)] += teamgoal1 - teamgoal2
		diff[names.index(teamname2)] += teamgoal2 - teamgoal1
	res = []
	for j in range(4):
		res.append([scores[j], diff[j], names[j]])
	res.sort()
	print(res[-1][-1], res[-2][-1])


t = int(input())
while t:
	ds = {}
	for i in range(12):
		ls = input().split(' ')
		if ls[0] not in ds:
			ds[ls[0]] = [int(ls[1]), int(ls[3]), 0]
		else:
			ds[ls[0]][0] += int(ls[1])
			ds[ls[0]][1] += int(ls[3])
		if ls[4] not in ds:
			ds[ls[4]] = [int(ls[3]), int(ls[1]), 0]
		else:
			ds[ls[4]][0] += int(ls[3])
			ds[ls[4]][1] += int(ls[1])
		if int(ls[1]) > int(ls[3]):
			ds[ls[0]][2] += 3
		elif int(ls[1]) < int(ls[3]):
			ds[ls[4]][2] += 3
		else:
			ds[ls[0]][2] += 1
			ds[ls[4]][2] += 1
	mVal = max(ds.values(), key=lambda x: x[2])
	key = 0
	for i in ds.keys():
		if ds[i][2] == mVal[2] and mVal[0] - mVal[1] < ds[i][0] - ds[i][1]:
			key = i
			mVal = ds[i]
		elif ds[i] == mVal:
			key = i
	one = key
	ds.pop(key)
	val = max(ds.values(), key=lambda x: x[2])
	for i in ds.keys():
		if ds[i][2] == val[2] and val[0] - val[1] < ds[i][0] - ds[i][1]:
			key = i
			val = ds[i]
		elif ds[i] == val:
			key = i
	two = key
	print(one, two)
	t -= 1


t = int(input())
for i in range(t):
	l = []
	for j in range(12):
		y = input()
		x = y.split()
		l.append(x)
	d = {}
	for j in range(12):
		if l[j][0] not in d:
			d[l[j][0]] = 0
		else:
			continue
	dg = d.copy()
	for j in range(12):
		if int(l[j][1]) > int(l[j][-2]):
			d[l[j][0]] += 3
		elif l[j][1] == l[j][-2]:
			d[l[j][0]] += 1
			d[l[j][-1]] += 1
		else:
			d[l[j][-1]] += 3
	for j in range(12):
		val = int(l[j][1]) - int(l[j][-2])
		dg[l[j][0]] += val
		val1 = int(l[j][-2]) - int(l[j][1])
		dg[l[j][-1]] += val1
	fl = []
	for i in d.keys():
		fl.append([d[i], dg[i], i])
	fl.sort(reverse=True)
	print(fl[0][2], fl[1][2])


t = int(input())
for _ in range(t):
	matches = []
	pt = {}
	for i in range(12):
		(t1, s1, vs, s2, t2) = input().split()
		s1 = int(s1)
		s2 = int(s2)
		if t1 not in pt:
			pt[t1] = [0, 0]
		if t2 not in pt:
			pt[t2] = [0, 0]
		if s1 > s2:
			pt[t1][0] += 3
		elif s1 < s2:
			pt[t2][0] += 3
		else:
			pt[t1][0] += 1
			pt[t2][0] += 1
		pt[t1][1] += s1 - s2
		pt[t2][1] += s2 - s1
	table = []
	for i in pt:
		table.append([pt[i][0], pt[i][1], i])
	table.sort(reverse=True)
	print(table[0][2], table[1][2])


t = int(input())
for i in range(t):
	names = []
	scores = [0] * 4
	diff = [0] * 4
	for j in range(12):
		(name1, goals1, bb, goals2, name2) = input().split()
		goals1 = int(goals1)
		goals2 = int(goals2)
		if name1 not in names:
			names.append(name1)
		if name2 not in names:
			names.append(name2)
		if goals1 > goals2:
			scores[names.index(name1)] += 3
		elif goals2 > goals1:
			scores[names.index(name2)] += 3
		else:
			scores[names.index(name1)] += 1
			scores[names.index(name2)] += 1
		diff[names.index(name1)] += goals1 - goals2
		diff[names.index(name2)] += goals2 - goals1
	res = []
	for j in range(4):
		res.append([scores[j], diff[j], names[j]])
	res.sort()
	print(res[-1][-1], res[-2][-1])


for _ in range(int(input())):
	d = {}
	for i in range(12):
		l = []
		l = list(map(str, input().split()))
		if l[0] not in d:
			d[l[0]] = [0, 0]
		if l[-1] not in d:
			d[l[-1]] = [0, 0]
		if int(l[1]) > int(l[3]):
			d[l[0]][0] += 3
		elif int(l[1]) < int(l[3]):
			d[l[-1]][0] += 3
		else:
			d[l[0]][0] += 1
			d[l[-1]][0] += 1
		d[l[0]][1] += int(l[1]) - int(l[3])
		d[l[-1]][1] += int(l[3]) - int(l[1])
	ans = []
	for i in d:
		ans.append([d[i][0], d[i][1], i])
	ans.sort(reverse=True)
	print(ans[0][2], ans[1][2])


for _ in range(int(input())):
	d = {}
	for i in range(12):
		(a, b, c, d1, e) = map(str, input().split(' '))
		if a not in d:
			d[a] = [0, 0]
		if e not in d:
			d[e] = [0, 0]
		b = int(b)
		d1 = int(d1)
		if b > d1:
			d[a][0] += 3
		elif b < d1:
			d[e][0] += 3
		elif b == d1:
			d[a][0] += 1
			d[e][0] += 1
		d[a][1] += b - d1
		d[e][1] += d1 - b
	ans = []
	for i in d:
		ans.append([d[i][0], d[i][1], i])
	ans = sorted(ans, reverse=True)
	print(ans[0][2], ans[1][2])


try:
	t = int(input())
	for i in range(t):
		t_name = []
		t_goal_score = [0, 0, 0, 0]
		t_goal_difference = [0, 0, 0, 0]
		for j in range(12):
			(t_x, t_x_s, i, t_y_s, t_y) = input().split()
			if t_x not in t_name:
				t_name.append(t_x)
			if t_y not in t_name:
				t_name.append(t_y)
			t_x_s = int(t_x_s)
			t_y_s = int(t_y_s)
			if t_x_s > t_y_s:
				t_goal_score[t_name.index(t_x)] += 3
			elif t_y_s > t_x_s:
				t_goal_score[t_name.index(t_y)] += 3
			else:
				t_goal_score[t_name.index(t_x)] += 1
				t_goal_score[t_name.index(t_y)] += 1
			t_goal_difference[t_name.index(t_x)] += t_x_s - t_y_s
			t_goal_difference[t_name.index(t_y)] += t_y_s - t_x_s
		final_matrix = []
		for k in range(4):
			final_matrix.append([t_goal_score[k], t_goal_difference[k], t_name[k]])
		final_matrix.sort(reverse=True)
		print(final_matrix[0][2], final_matrix[1][2])
except:
	pass


t = int(input())
for _ in range(t):
	dic = {}
	for i in range(12):
		(t1, g1, vs, g2, t2) = input().split()
		(g1, g2) = (int(g1), int(g2))
		if g1 > g2:
			if t1 not in dic:
				dic[t1] = [3, g1 - g2]
			else:
				dic[t1][0] += 3
				dic[t1][1] += g1 - g2
			if t2 not in dic:
				dic[t2] = [0, g2 - g1]
			else:
				dic[t2][1] += g2 - g1
		elif g2 > g1:
			if t1 not in dic:
				dic[t1] = [0, g1 - g2]
			else:
				dic[t1][1] += g1 - g2
			if t2 not in dic:
				dic[t2] = [3, g2 - g1]
			else:
				dic[t2][0] += 3
				dic[t2][1] += g2 - g1
		else:
			if t1 not in dic:
				dic[t1] = [1, 0]
			else:
				dic[t1][0] += 1
			if t2 not in dic:
				dic[t2] = [1, 0]
			else:
				dic[t2][0] += 1
	arr = []
	for i in range(2):
		(temp, win, gd) = (0, '2', -1000000)
		for ele in dic:
			if dic[ele][0] > temp:
				temp = dic[ele][0]
				win = ele
				gd = dic[ele][1]
			elif dic[ele][0] == temp:
				if dic[ele][1] > gd:
					win = ele
					gd = dic[ele][1]
		arr.append(win)
		del dic[win]
	for ele in arr:
		print(ele, end=' ')
	print()


from collections import defaultdict as dd
t = int(input())
for _ in range(t):
	goalDiff = dd(int)
	points = dd(int)
	for lines in range(1, 13):
		(team1, goals1, vs, goals2, team2) = input().split()
		goals1 = int(goals1)
		goals2 = int(goals2)
		goalDiff[team1] += goals1
		goalDiff[team1] -= goals2
		goalDiff[team2] += goals2
		goalDiff[team2] -= goals1
		if goals1 == goals2:
			points[team1] += 1
			points[team2] += 1
		elif goals1 > goals2:
			points[team1] += 3
		else:
			points[team2] += 3
	finalMatrix = []
	for index in points:
		finalMatrix.append([points[index], goalDiff[index], index])
	finalMatrix.sort(reverse=True)
	print(finalMatrix[0][2], finalMatrix[1][2])


tcase = int(input())
while tcase > 0:
	pointdict = {}
	gddict = {}
	for q in range(12):
		match = input().split(' vs. ')
		score = []
		team = []
		for i in match:
			for j in i.split():
				if j.isdigit():
					score.append(int(j))
				elif j.isalpha():
					team.append(j)
		for i in range(len(team)):
			if team[i] not in pointdict:
				pointdict[team[i]] = 0
			if team[i] not in gddict:
				gddict[team[i]] = 0
		for i in score:
			i = int(i)
		if score[0] > score[1]:
			pointdict[team[0]] += 3
			gddict[team[0]] += score[0] - score[1]
			gddict[team[1]] += score[1] - score[0]
		elif score[1] > score[0]:
			pointdict[team[1]] += 3
			gddict[team[0]] += score[0] - score[1]
			gddict[team[1]] += score[1] - score[0]
		else:
			pointdict[team[1]] += 1
			pointdict[team[0]] += 1
			gddict[team[0]] += score[0] - score[1]
			gddict[team[1]] += score[1] - score[0]
	secpoint = 0
	scores = []
	gds = []
	teams = []
	for j in pointdict.values():
		scores.append(j)
	for k in gddict.values():
		gds.append(k)
	for l in pointdict.keys():
		teams.append(l)
	maxpoint = max(scores)
	if scores.count(maxpoint) > 1:
		secpoint = maxpoint
	else:
		for i in range(len(scores)):
			if scores[i] > secpoint and scores[i] < maxpoint:
				secpoint = scores[i]
	finalmax = ''
	finalsecmax = ''
	if scores.count(maxpoint) == 1:
		finalmax += teams[scores.index(maxpoint)]
	else:
		s = []
		for i in teams:
			if scores[teams.index(i)] == maxpoint:
				s.append(i)
		g = []
		for i in s:
			g.append(gds[teams.index(i)])
		finalmax += teams[gds.index(max(g))]
	if scores.count(secpoint) == 1:
		finalsecmax += teams[scores.index(secpoint)]
	else:
		s = []
		for i in teams:
			if scores[teams.index(i)] == secpoint:
				s.append(i)
		g = []
		for i in s:
			if scores[teams.index(i)] == secpoint and gds[teams.index(i)] != gds[teams.index(finalmax)]:
				g.append(gds[teams.index(i)])
		finalsecmax += teams[gds.index(max(g))]
	print(finalmax, finalsecmax)
	tcase -= 1


n = int(input())
for i in range(0, n):
	d = {}
	d2 = {}
	for j in range(0, 12):
		s = input()
		s = s.split(' ')
		if int(s[1]) > int(s[3]):
			if s[0] in d.keys():
				d[s[0]] += 3
			else:
				d[s[0]] = 3
			if s[0] in d2.keys():
				d2[s[0]] += int(s[1]) - int(s[3])
			else:
				d2[s[0]] = int(s[1]) - int(s[3])
			if s[4] in d2.keys():
				d2[s[4]] -= int(s[1]) - int(s[3])
			else:
				d2[s[4]] = -(int(s[1]) - int(s[3]))
		elif int(s[1]) < int(s[3]):
			if s[4] in d.keys():
				d[s[4]] += 3
			else:
				d[s[4]] = 3
			if s[4] in d2.keys():
				d2[s[4]] += int(s[3]) - int(s[1])
			else:
				d2[s[4]] = int(s[3]) - int(s[1])
			if s[0] in d2.keys():
				d2[s[0]] -= int(s[3]) - int(s[1])
			else:
				d2[s[0]] = -(int(s[3]) - int(s[1]))
		elif int(s[1]) == int(s[3]):
			if s[0] in d.keys():
				d[s[0]] += 1
			else:
				d[s[0]] = 1
			if s[4] in d.keys():
				d[s[4]] += 1
			else:
				d[s[4]] = 1
	st = ''
	for l in range(0, 2):
		max = 0
		win = ''
		for k in d.keys():
			if d[k] > max:
				max = d[k]
				win = k
			elif d[k] == max:
				if d2[k] > d2[win]:
					max = d[k]
					win = k
		st = st + ' ' + win
		del d[win]
		del d2[win]
	print(st.strip())


n = int(input())
for i in range(0, n):
	d = {}
	for j in range(0, 12):
		s = input()
		s = s.split(' ')
		if int(s[1]) > int(s[3]):
			if s[0] in d.keys():
				d[s[0]][0] += 3
				d[s[0]][1] += int(s[1]) - int(s[3])
			else:
				d[s[0]] = [3, int(s[1]) - int(s[3])]
			if s[4] in d.keys():
				d[s[4]][1] -= int(s[1]) - int(s[3])
			else:
				d[s[4]] = [0, -(int(s[1]) - int(s[3]))]
		elif int(s[1]) < int(s[3]):
			if s[4] in d.keys():
				d[s[4]][0] += 3
				d[s[4]][1] += int(s[3]) - int(s[1])
			else:
				d[s[4]] = [3, int(s[3]) - int(s[1])]
			if s[0] in d.keys():
				d[s[0]][1] -= int(s[3]) - int(s[1])
			else:
				d[s[0]] = [0, -(int(s[3]) - int(s[1]))]
		elif int(s[1]) == int(s[3]):
			if s[0] in d.keys():
				d[s[0]][0] += 1
			else:
				d[s[0]] = [1, 0]
			if s[4] in d.keys():
				d[s[4]][0] += 1
			else:
				d[s[4]] = [1, 0]
	d = sorted(d, key=d.get, reverse=True)
	print(d[0], d[1])


for _ in range(int(input())):
	di = {}
	for i in range(12):
		a = input()
		arr = a.split(' ')
		if arr[0] not in di:
			di[arr[0]] = 0
		if arr[-1] not in di:
			di[arr[-1]] = 0
		if int(arr[1]) > int(arr[3]):
			di[arr[0]] += 3000
		elif int(arr[1]) < int(arr[3]):
			di[arr[-1]] += 3000
		else:
			di[arr[0]] += 1000
			di[arr[-1]] += 1000
		di[arr[-1]] += int(arr[3]) - int(arr[1])
		di[arr[0]] += int(arr[1]) - int(arr[3])
	sortedaa = sorted(di.items(), key=lambda x: x[1], reverse=True)
	print(sortedaa[0][0], sortedaa[1][0])


for _ in range(int(input())):
	a = {}
	for i in range(12):
		(b, c, d, e) = input().replace('vs. ', '').split()
		if b not in a.keys():
			a[b] = 0
		if e not in a.keys():
			a[e] = 0
		c = int(c)
		d = int(d)
		if c > d:
			a[b] += 3000 + c - d
			a[e] += d - c
		elif c < d:
			a[b] += c - d
			a[e] += 3000 + d - c
		else:
			a[b] += 1000
			a[e] += 1000
	f = sorted(a.items(), key=lambda x: x[1], reverse=True)
	print(f[0][0], f[1][0])


subtasks = int(input())
for _ in range(subtasks):
	team = {}
	for i in range(12):
		(H, Hs, Vs, As, A) = input().split()
		Hs = int(Hs)
		As = int(As)
		if H not in team:
			team[H] = [0, 0]
		if A not in team:
			team[A] = [0, 0]
		if Hs > As:
			team[H][0] += 3
			team[H][1] += Hs - As
			team[A][1] += As - Hs
		elif As > Hs:
			team[A][0] += 3
			team[A][1] += As - Hs
			team[H][1] += Hs - As
		else:
			team[H][0] += 1
			team[A][0] += 1
	ans = sorted(team.items(), key=lambda x: x[1], reverse=True)
	print(ans[0][0], ans[1][0])


for _ in range(int(input())):
	xd = {}
	for _ in range(12):
		(x, xx, _, yy, y) = input().split()
		if x not in xd:
			xd[x] = [0, 0]
		if y not in xd:
			xd[y] = [0, 0]
		(xx, yy) = (int(xx), int(yy))
		if xx > yy:
			xd[x][0] += 3
			xd[x][1] += xx - yy
			xd[y][1] -= xx - yy
		elif xx == yy:
			xd[x][0] += 1
			xd[y][0] += 1
		elif xx < yy:
			xd[y][0] += 3
			xd[y][1] += yy - xx
			xd[x][1] -= yy - xx
	xd = sorted(xd.items(), key=lambda item: item[1], reverse=True)
	print(xd[0][0], xd[1][0])


t = int(input())
for i in range(t):
	d = {}
	for i in range(12):
		(a, b, c, p, e) = map(str, input().split())
		(b, p) = (int(b), int(p))
		if a not in d:
			d[a] = [0, 0]
		if e not in d:
			d[e] = [0, 0]
		if b > p:
			d[a][0] += 3
			d[a][1] += b - p
			d[e][1] -= b - p
		elif p > b:
			d[e][0] += 3
			d[e][1] += p - b
			d[a][1] -= p - b
		else:
			d[a][0] += 1
			d[e][0] += 1
	d = sorted(d, key=d.get)
	print(d[3], d[2])


for _ in range(int(input())):
	d = {}
	for i in range(12):
		(a, b) = map(str, input().split('vs. '))
		a = a.split()
		b = b.split()
		if a[0] not in d:
			d[a[0]] = [0, 0]
		if b[1] not in d:
			d[b[1]] = [0, 0]
		diff_a = int(a[1]) - int(b[0])
		d[a[0]][1] += diff_a
		diff_b = int(b[0]) - int(a[1])
		d[b[1]][1] += diff_b
		if diff_a > diff_b:
			d[a[0]][0] += 3
		elif diff_b > diff_a:
			d[b[1]][0] += 3
		else:
			d[a[0]][0] += 1
			d[b[1]][0] += 1
	d = sorted(d, key=d.get)
	print(d[3], d[2])


import sys
import math
from collections import defaultdict as dd
from sys import stdin
input = stdin.readline
m = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)
T = int(input())
for i in range(T):
	(tp, tg) = (dd(int), dd(int))
	for i in range(12):
		l = input().split()
		(sc1, sc2) = (int(l[1]), int(l[3]))
		if sc1 > sc2:
			tp[l[0]] += 3
			tp[l[4]] += 0
			tg[l[0]] += sc1 - sc2
			tg[l[4]] += sc2 - sc1
		elif sc1 < sc2:
			tp[l[0]] += 0
			tp[l[4]] += 3
			tg[l[0]] += sc1 - sc2
			tg[l[4]] += sc2 - sc1
		else:
			tp[l[0]] += 1
			tp[l[4]] += 1
			tg[l[0]] += sc1 - sc2
			tg[l[4]] += sc2 - sc1
	mat = []
	for i in tp:
		mat.append([tp[i], tg[i], i])
	mat.sort(reverse=True)
	(x, y) = (mat[0][2], mat[1][2])
	print(x, y)


def tableToppers(scoreBoard):
	sorted_dict = {}
	sorted_keys = sorted(scoreBoard, key=scoreBoard.get)
	for w in sorted_keys:
		sorted_dict[w] = scoreBoard[w]
	print(list(sorted_dict)[-1], '', list(sorted_dict)[-2])

def updateTeam(scoreBoard, Name, Goals, Conceded):
	if Name not in scoreBoard.keys():
		list1 = []
		if Goals > Conceded:
			list1.append(3)
		elif Goals == Conceded:
			list1.append(1)
		else:
			list1.append(0)
		scoreBoard[Name] = list1
		list1.append(Goals - Conceded)
	else:
		scoreBoard[Name][1] += Goals - Conceded
		if Goals > Conceded:
			scoreBoard[Name][0] += 3
		elif Goals == Conceded:
			scoreBoard[Name][0] += 1
for _ in range(int(input())):
	scoreBoard = {}
	for i in range(12):
		(HName, HGoals, vs, AGoals, AName) = input().split()
		updateTeam(scoreBoard, HName.strip(), int(HGoals), int(AGoals))
		updateTeam(scoreBoard, AName.strip(), int(AGoals), int(HGoals))
	tableToppers(scoreBoard)


T = int(input())
for _ in range(T):
	points = {}
	for i in range(12):
		l = input().split(' ')
		scoreA = int(l[1])
		scoreB = int(l[3])
		if scoreA > scoreB:
			(pointsA, pointsB) = (3, 0)
		elif scoreA < scoreB:
			(pointsA, pointsB) = (0, 3)
		else:
			(pointsA, pointsB) = (1, 1)
		c = points.get(l[0], [0, 0])
		points[l[0]] = [c[0] + pointsA, c[1] + scoreA - scoreB]
		c = points.get(l[-1], [0, 0])
		points[l[-1]] = [c[0] + pointsB, c[1] + scoreB - scoreA]
	scores = [(a, b) for (b, a) in points.items()]
	scores.sort()
	print(scores[-1][1], scores[-2][1])


for i in range(int(input())):
	d = {}
	for j in range(12):
		str = input()
		l = str.split()
		team1 = l[0]
		team2 = l[4]
		x = int(l[1])
		y = int(l[3])
		t1 = []
		t2 = []
		if team1 not in d.keys():
			d[team1] = [0, 0]
		if team2 not in d.keys():
			d[team2] = [0, 0]
		if x > y:
			d[team1][0] += 3
		elif y > x:
			d[team2][0] += 3
		else:
			d[team1][0] += 1
			d[team2][0] += 1
		d[team1][1] += x - y
		d[team2][1] += y - x
	sort_sict = sorted(d.items(), key=lambda x: (x[1][0], x[1][1]), reverse=True)
	j = 0
	print(sort_sict[j][0], sort_sict[j + 1][0])


t = int(input())
for _ in range(t):
	n = 12
	d = {}
	for _ in range(n):
		(teamA, teamB) = input().split(' vs. ')
		teamA = teamA.split()
		teamB = teamB.split()
		if teamA[0] not in d:
			d.update({teamA[0]: [0, 0]})
		if teamB[1] not in d:
			d.update({teamB[1]: [0, 0]})
		goalA = int(teamA[1]) - int(teamB[0])
		goalB = int(teamB[0]) - int(teamA[1])
		d[teamA[0]][1] += goalA
		d[teamB[1]][1] += goalB
		if goalA > goalB:
			d[teamA[0]][0] += 3
		elif goalB > goalA:
			d[teamB[1]][0] += 3
		else:
			d[teamA[0]][0] += 1
			d[teamB[1]][0] += 1
	result = sorted(d, key=d.get)
	print(result[-1], result[-2])


test_cases = int(input())

def pointstable(team1detail, team2detail, table):
	if team1detail[0] not in table:
		table.append(team1detail[0])
		pointtable[team1detail[0]] = [0, 0]
	else:
		pass
	if team2detail[1] not in table:
		table.append(team2detail[1])
		pointtable[team2detail[1]] = [0, 0]
	else:
		pass
	team1goal = int(team1detail[1].strip())
	team2goal = int(team2detail[0].strip())
	if team1goal > team2goal:
		pointtable[team1detail[0]][0] += 3
		pointtable[team1detail[0]][1] += team1goal - team2goal
		pointtable[team2detail[1]][1] += team2goal - team1goal
	elif team1goal == team2goal:
		pointtable[team1detail[0]][0] += 1
		pointtable[team2detail[1]][0] += 1
	else:
		pointtable[team2detail[1]][0] += 3
		pointtable[team1detail[0]][1] += team1goal - team2goal
		pointtable[team2detail[1]][1] += team2goal - team1goal

def result(matchdetail, table):
	team1 = matchdetail[0].strip()
	team2 = matchdetail[1].strip()
	team1detail = team1.split(' ')
	team2detail = team2.split(' ')
	pointstable(team1detail, team2detail, table)
for i in range(test_cases):
	table = []
	pointtable = {}
	for matches in range(12):
		inp = input().split('vs.')
		result(inp, table)
	sorted_table = {}
	sorted_points = sorted(pointtable, key=pointtable.get)
	for w in sorted_points:
		sorted_table[w] = pointtable[w]
	listoftabletop = []
	for key in sorted_table.keys():
		listoftabletop.append(key)
	print(listoftabletop[3], listoftabletop[2])


T = int(input())
for _ in range(T):
	fixtures = []
	team_names = []
	for j in range(12):
		fixture = input().split()
		fixtures.append(fixture)
		team_names.append(fixture[0])
	fixt_dict = dict([(name, 0) for name in set(team_names)])
	for team in fixt_dict.keys():
		total_diff = 0
		total_points = 0
		fixt_dict[team] = dict([('points', total_points), ('goal_diff', total_diff)])
		for fix in fixtures:
			home_goal = int(fix[1])
			away_goal = int(fix[3])
			goal_diff = 0
			if team == fix[0]:
				goal_diff = home_goal - away_goal
			elif team == fix[4]:
				goal_diff = away_goal - home_goal
			if team == fix[0] or team == fix[4]:
				point_alloc = 0
				if goal_diff > 0:
					point_alloc = 3
				elif goal_diff == 0:
					point_alloc = 1
				total_points += point_alloc
				total_diff += goal_diff
		fixt_dict[team]['goal_diff'] = total_diff
		fixt_dict[team]['points'] = total_points
	sorted_fict = dict(sorted(fixt_dict.items(), key=lambda tup: (tup[1]['points'], tup[1]['goal_diff']), reverse=True))
	table = [k for k in sorted_fict.keys()]
	print(' '.join(table[:2]))


def init_func():
	return {'pts': 0, 'gd': 0}
from collections import defaultdict
for _ in range(int(input())):
	ref_dict = defaultdict(init_func)
	(max_pt1, max_gd1, max_pt2, max_gd2, max_elem1, max_elem2) = (0, 0, 0, 0, '', '')
	for j in range(12):
		tmp_arr = tuple((tuple(i.strip().split()) for i in input().strip().split('vs.')))
		team1 = tmp_arr[0]
		team1_nm = team1[0]
		team1_g = int(team1[1])
		team2 = tmp_arr[1]
		team2_nm = team2[1]
		team2_g = int(team2[0])
		gd = team1_g - team2_g
		if gd > 0:
			ref_dict[team1_nm]['pts'] += 3
		elif gd < 0:
			ref_dict[team2_nm]['pts'] += 3
		else:
			ref_dict[team1_nm]['pts'] += 1
			ref_dict[team2_nm]['pts'] += 1
		ref_dict[team1_nm]['gd'] += gd
		ref_dict[team2_nm]['gd'] -= gd
	for elem in ref_dict.keys():
		currPts = ref_dict[elem]['pts']
		currGD = ref_dict[elem]['gd']
		if currPts > max_pt1 or (currPts == max_pt1 and currGD > max_gd1):
			max_elem2 = max_elem1
			max_pt2 = max_pt1
			max_gd2 = max_gd1
			max_elem1 = elem
			max_pt1 = currPts
			max_gd1 = currGD
		elif currPts > max_pt2 or (currPts == max_pt2 and currGD > max_gd2):
			max_elem2 = elem
			max_pt2 = currPts
			max_gd2 = currGD
	print(max_elem1, max_elem2)


for i in range(int(input())):
	d = dict()
	for j in range(12):
		l = input().split()
		l.remove('vs.')
		if l[0] in d:
			if int(l[1]) > int(l[2]):
				d[l[0]] = (d[l[0]][0] + 3, d[l[0]][1] + int(l[1]) - int(l[2]))
			elif int(l[1]) < int(l[2]):
				d[l[0]] = (d[l[0]][0], d[l[0]][1] + int(l[1]) - int(l[2]))
			else:
				d[l[0]] = (d[l[0]][0] + 1, d[l[0]][1] + int(l[1]) - int(l[2]))
		elif l[0] not in d:
			if int(l[1]) > int(l[2]):
				d[l[0]] = (3, int(l[1]) - int(l[2]))
			elif int(l[1]) < int(l[2]):
				d[l[0]] = (0, int(l[1]) - int(l[2]))
			else:
				d[l[0]] = (1, int(l[1]) - int(l[2]))
		if l[3] in d:
			if int(l[2]) > int(l[1]):
				d[l[3]] = (d[l[3]][0] + 3, d[l[3]][1] + int(l[2]) - int(l[1]))
			elif int(l[2]) < int(l[1]):
				d[l[3]] = (d[l[3]][0], d[l[3]][1] + int(l[2]) - int(l[1]))
			else:
				d[l[3]] = (d[l[3]][0] + 1, d[l[3]][1] + int(l[2]) - int(l[1]))
		elif l[3] not in d:
			if int(l[2]) > int(l[1]):
				d[l[3]] = (3, int(l[2]) - int(l[1]))
			elif int(l[1]) > int(l[2]):
				d[l[3]] = (0, int(l[2]) - int(l[1]))
			else:
				d[l[3]] = (1, int(l[2]) - int(l[1]))
	l = list()
	for (k, v) in d.items():
		l.append((v, k))
	l.sort()
	print(l[-1][1], l[-2][1])