// Arup Guha
// 7/20/2015
// Solution to 2010 UCF Fall Locals 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();

		// Process all cases.
		for (int loop=1; loop<=numCases; loop++) {

			// Set up storage.
			int n = stdin.nextInt();
			int games = stdin.nextInt();
			HashMap<String,Integer> teamList = new HashMap<String,Integer>();
			team[] list = new team[n];
			for (int i=0; i<n; i++) {
				String name = stdin.next();
				list[i] = new team(name);
				teamList.put(name, i);
			}

			// Tally games.
			for (int i=0; i<games; i++) {
				String t1 = stdin.next();
				int g1 = stdin.nextInt();
				String t2 = stdin.next();
				int g2 = stdin.nextInt();
				update(list, teamList, t1, g1, t2, g2);
			}

			// Sort teams.
			Arrays.sort(list);

			// Print result.
			System.out.println("Group "+loop+":");
			for (int i=0; i<n; i++)
				System.out.println(list[i]);
			System.out.println();
		}
	}

	// Updates list to account for the match between t1 and t2 who scored g1 and g2 goals, respectively.
	public static void update(team[] list, HashMap<String,Integer> map, String t1, int g1, String t2, int g2) {

		// Carry out three cases manually.
		if (g1 > g2) {
			list[map.get(t1)].wins++;
			list[map.get(t2)].losses++;
		}
		else if (g1 < g2) {
			list[map.get(t2)].wins++;
			list[map.get(t1)].losses++;
		}
		else {
			list[map.get(t1)].ties++;
			list[map.get(t2)].ties++;
		}

		// This stuff is the same...
		list[map.get(t1)].goals += g1;
		list[map.get(t2)].goalsAgainst += g1;
		list[map.get(t2)].goals += g2;
		list[map.get(t1)].goalsAgainst += g2;
	}
}

class team implements Comparable<team> {

	public String name;
	public int wins;
	public int ties;
	public int losses;
	public int goals;
	public int goalsAgainst;

	public team(String n) {
		name = n;
		wins = 0;
		losses = 0;
		goals = 0;
		goalsAgainst = 0;
	}

	public int points() {
		return 3*wins + ties;
	}

	public int diff() {
		return goals - goalsAgainst;
	}

	// Implement annoying rules.
	public int compareTo(team other) {
		if (this.points() != other.points()) return other.points() - this.points();
		if (this.diff() != other.diff()) return other.diff() - this.diff();
		if (this.goals != other.goals) return other.goals - this.goals;
		return this.name.compareTo(other.name);
	}

	public String toString() {
		return name+" "+ points() + " " + wins + " " + losses + " " + ties + " " + goals + " " + goalsAgainst;
	}
}