public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in,
				"ISO-8859-1"));
		String line = "";
		StringBuilder out = new StringBuilder();
		OutputStreamWriter cout = new OutputStreamWriter(System.out,
				"ISO-8859-1");
		d: do {
			line = br.readLine();
			if (line == null || line.length() == 0)
				break d;
			int times = Integer.parseInt(line);
			String nameCup = "";
			for (int i = 0; i < times; i++) {
				nameCup = br.readLine();
				int nteams = Integer.parseInt(br.readLine());
				HashMap<String, Integer> mapTeams = new HashMap<String, Integer>(
						nteams);
				Team[] teams = new Team[nteams];
				int[][] valuesTeams = new int[nteams][8];
				for (int j = 0; j < nteams; j++) {
					mapTeams.put(line = br.readLine(), j);
					teams[j] = new Team(line);
				}
				int nmatches = Integer.parseInt(br.readLine());
				for (int j = 0; j < nmatches; j++) {
					line = br.readLine();
					String[] nameTeams = nameTeams(line);
					int[] goals = goals(line);
					int t1 = mapTeams.get(nameTeams[0]), t2 = mapTeams
							.get(nameTeams[1]);
					teams[t1].match(goals[0], goals[1]);
					teams[t2].match(goals[1], goals[0]);
				}
				Arrays.sort(teams);
				if (i != 0)
					out.append("\n");
				out.append(nameCup).append("\n");
				for (int j = 0; j < valuesTeams.length; j++)
					out.append((j + 1) + ") " + teams[j] + "\n");
			}

		} while (line != null && line.length() != 0);
		cout.write(out.toString());
		cout.flush();
	}