// Arup Guha
// 2/18/2024
// Solution to Kattis Problem: Closing the Loop
// https://open.kattis.com/problems/closingtheloop

import java.util.*;

public class closingtheloop {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
		
			ArrayList<Integer> blue = new ArrayList<Integer>();
			ArrayList<Integer> red = new ArrayList<Integer>();
			int n = stdin.nextInt();
			
			// Read through pieces of string.
			for (int i=0; i<n; i++) {
			
				// Get string and its length.
				String item = stdin.next();
				int sLen = item.length();
				
				// Parse out length of the rope.
				int len = Integer.parseInt(item.substring(0, sLen-1));
				
				// Add this length to the appropriate list.
				if (item.charAt(sLen-1) == 'B')
					blue.add(len);
				else
					red.add(len);
			}
			
			// How many we take from each list.
			int take = Math.min(red.size(), blue.size());
			
			// Sort both from big to small.
			red.sort(Collections.reverseOrder());
			blue.sort(Collections.reverseOrder());
			
			// Add lengths of string to use.
			int res = 0;
			for (int i=0; i<take; i++)
				res += (red.get(i)+blue.get(i));
				
			// For knots.
			res -= 2*take;
			System.out.println("Case #"+loop+": "+res);
		}
	}
}