// Arup Guha
// 5/10/2015
// Solution to FHSPS Problem: Soccer Standings

import java.util.*;

public class soccer {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in the teams, storing a backwards map.
			int n = stdin.nextInt();
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			team[] league = new team[n];
			for (int i=0; i<n; i++) {
				String myTeam = stdin.next();
				map.put(myTeam, i);
				league[i] = new team(myTeam);
			}

			// Process games.
			int games = stdin.nextInt();
			for (int i=0; i<games; i++) {

				// Read in this game, getting the team number.
				int teamOne = map.get(stdin.next());
				int goalsOne = stdin.nextInt();
				int teamTwo = map.get(stdin.next());
				int goalsTwo = stdin.nextInt();

				// Record game for both teams.
				league[teamOne].addGame(goalsOne, goalsTwo);
				league[teamTwo].addGame(goalsTwo, goalsOne);
			}

			// Sort the league in the desired order.
			Arrays.sort(league);

			// Display results as desired.
			System.out.println("League #"+loop+":");
			for (int i=0; i<n; i++)
				System.out.println(league[i]);
			System.out.println();
		}
	}
}

class team implements Comparable<team> {

	public String name;
	public int wins;
	public int ties;
	public int losses;
	public int goalsFor;
	public int goalsAgainst;

	public team(String myName) {
		name = myName;
		wins = 0;
		ties = 0;
		losses = 0;
		goalsFor = 0;
		goalsAgainst = 0;
	}

	// Output for a team desired by this problem.
	public String toString() {
		return name;
	}

	// Adds a game for this team who scores scored goals and the opponent
	// scores against goals.
	public void addGame(int scored, int against) {

		// Record W/L/T.
		if (scored > against) 		wins++;
		else if (scored == against) ties++;
		else						losses++;

		// Add goals.
		goalsFor += scored;
		goalsAgainst += against;
	}

	// Returns the total number of games played by this team.
	public int numGames() {
		return wins+losses+ties;
	}

	// Returns the number of points earned by this team.
	public int points() {
		return 3*wins + ties;
	}

	// Returns the current goal differential for this team.
	public int goalDiff() {
		return goalsFor - goalsAgainst;
	}

	// Returns a negative int if this team is better than other in the standards,
	// a positive integers if it's worse and zero otherwise.
	public int compareTo(team other) {

		// Sort by points, if different.
		if (this.points() != other.points())
			return other.points() - this.points();

		// Then wins.
		if (this.wins != other.wins)
			return other.wins - this.wins;

		// Then goal differential.
		if (this.goalDiff() != other.goalDiff())
			return other.goalDiff() - this.goalDiff();

		// Last rule =)
		return other.goalsFor - this.goalsFor;
	}
}