// Arup Guha
// 4/25/2019
// Solution to 2019 FHSPS Playoff Problem: Harkness Rating System

import java.util.*;

public class harkness {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();
		
		// Process each case.
		for (int loop=0; loop<numCases; loop++) {
			
			// Read in the players.
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			int numP = stdin.nextInt();
			player[] players = new player[numP]; 
			for (int i=0; i<numP; i++) {
				String name = stdin.next();
				int rating = stdin.nextInt();
				players[i] = new player(name, i, rating);
				map.put(name, i);
			}
			
			// Process each match.
			int numM = stdin.nextInt();
			for (int i=0; i<numM; i++) {
				
				// Get player IDs.
				int p1 = map.get(stdin.next());
				int p2 = map.get(stdin.next());
				
				// Get result and update statistics.
				char res = stdin.next().charAt(0);
				players[p1].addResult(players[p2], res);
			}
			
			// Update everyone's ranking now that the tournament is over.
			for (int i=0; i<numP; i++) 
				players[i].updateRanking();
			
			// Sort the list!
			Arrays.sort(players);
			
			// Ta da!
			for (int i=0; i<numP; i++)
				System.out.println(players[i]);
		}
	}
}

class player implements Comparable<player> {

	public String name;
	public int ID;
	public int rating;
	public int points;
	public int possiblePoints;
	public ArrayList<Integer> oppRatings;
	
	public player(String s, int myid, int myrating) {
		name = s;
		ID = myid;
		rating = myrating;
		points = 0;
		possiblePoints = 0;
		oppRatings = new ArrayList<Integer>();
	}
	
	// This is how the problem wants the output.
	public String toString() {
		return name+" "+rating;
	}
	
	// Just doing it this way (L = 0, T = 1, W = 2) so I have all ints.
	public void addResult(player opponent, char c) {
		
		// Possible points for both teams.
		possiblePoints += 2;
		opponent.possiblePoints += 2;
		
		// Each was each other's rating.
		oppRatings.add(opponent.rating);
		opponent.oppRatings.add(rating);
		
		// Now, adjust the points.
		if (c == 'W') points += 2;
		else if (c == 'L') opponent.points += 2;
		else {
			points += 1;
			opponent.points += 1;
		}
	}
	
	public double winPerc() {
		return 100.0*points/possiblePoints;
	}
	
	// This implements the rules given in the problem: sort by rating high to low, 
	// and for ties do ID low to high.
	public int compareTo(player other) {
		if (rating != other.rating)
			return other.rating - rating;
		return ID - other.ID;
	}
	
	// Updates ranking based on all the games played since the last update Ranking...
	public void updateRanking() {
		
		// First calculate your opponents ratings.
		double avg = 0;
		for (Integer x: oppRatings)
			avg += x;
		avg /= oppRatings.size();
		
		// This is the formula given in the problem.
		rating = (int)(avg + 10*(winPerc()-50) + 1e-9);
		
		// Since rating has been updated, clear game history.
		points = 0;
		possiblePoints = 0;
		oppRatings.clear();
	}
}