#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
typedef struct inputs {
long number;
char** segments;
}inputs;
//Find the maximum lenght of the rope loop possible
long maximum_length(inputs ropes) {
vector<long> blue;
vector<long> red;
long count_blue = 0, count_red = 0 , total_length = 0;
for (long i = 0; i < ropes.number; i ++ ) {
red.push_back(0);
blue.push_back(0);
if (ropes.segments[i][1] == 'R') {
red[i] += (ropes.segments[i][0] - '0');
count_red++;
}
else if (ropes.segments[i][1] == 'B') {
blue[i] += (ropes.segments[i][0] - '0');
count_blue++;
}
}
if (count_red == 0 || count_blue == 0) {
return 0;
} else {
sort(blue.begin(), blue.begin()+count_blue);
sort(red.begin(),red.begin()+count_red);
if (count_red < count_blue) {
for (long i = 0; i < count_red; i++) {
total_length += blue[count_blue - i - 1] + red[i];
}
} else {
for (long i = 0; i < count_blue; i++) {
total_length += red[count_red - i - 1] + blue[i];
}
}
return total_length;
}
}
//Starting with the blue, add a red and a blue, alternatively until u end on a red
//and the number of blue and red is the same... (rope loop)
//Store the lengths of the blue ropes and red ropes each in a sorted array
int main () {
long no_cases;
long maximum_length(inputs ropes);
cin >> no_cases;
inputs* ropes;
ropes = (inputs*) calloc(no_cases, sizeof(inputs));
//Read the inputs and stores them into an array of struct
for (long i = 0; i < no_cases; i ++ ) {
cin >> ropes[i].number;
ropes[i].segments = (char**)calloc(ropes[i].number, sizeof(char*));
for (long j = 0; j < ropes[i].number; j ++ ) {
cin >> ropes[i].segments[j];
}
}
//For each case, prints the maximum length of the rope
for (long i = 0; i < no_cases; i ++) {
cout << "Case #" << i + 1 << ":" << maximum_length(ropes[i]) << endl;
}
return 0;
}