import java.io.*;
import java.util.*;

public class Problem2 {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(new File("DATA2.txt"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("OUT2.txt")));
		String get = "";
		String[] flights = new String[20];
		String next = "";
		int cost = 0;
		int cost1 = 0;
		for (int x=0; x<5; x++){
			cost = 0;
			get = sc.nextLine();
			if (get.charAt(0) > 47 && get.charAt(0) < 58){
				for (int y = 0; y< ((int)get.charAt(0) - 48); y++){
					flights[y] = sc.nextLine();

				}
				for (int y = 0; y< ((int)get.charAt(0) - 48); y++){
					if (flights[y].indexOf("YYZ") ==0){
						cost = 0;
						cost = cost + (int)Double.parseDouble(flights[y].substring(8, 11));
						next = flights[y].substring(4, 7);
					}
					if (flights[y].indexOf("SEA") ==4){
						if (flights[y].indexOf("YYZ") != 0){
							cost = cost + (int)Double.parseDouble(flights[y].substring(8, 11));
						}
						else if (flights[y].indexOf(next)== 0) {
							cost = cost + (int)Double.parseDouble(flights[y].substring(8, 11));
						}
					}
					if (flights[y].substring(0, 3).compareTo(next)==0){
						if (flights[y].indexOf("SEA") !=4){
						cost = cost + (int)Double.parseDouble(flights[y].substring(8, 11));
						}
					}
				}
				out.write("" + cost + "\n");
			}
		}
		sc.close();
		out.close();
		System.exit(0);
	}
	

}
