// Author: Adrian Kuegel
// Date: November 4th, 2010
// Algorithm: Dynamic Programming
// Complexity: O(n^2)

import java.io.*;
import java.util.*;

public class adrian {
	public static void main(String [] args) throws Exception{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int tc = Integer.parseInt(in.readLine());
		while(tc-- > 0) {
			int n = Integer.parseInt(in.readLine());
			String name = in.readLine();
			int jan_start = name.compareTo("Jan") == 0? 1 : 0;
			int [] petra_val = new int[n];
			int [] jan_val = new int[n];
			for (int i=0; i<n; ++i) {
				StringTokenizer strtok = new StringTokenizer(in.readLine());
				petra_val[i] = Integer.parseInt(strtok.nextToken());
				jan_val[i] = Integer.parseInt(strtok.nextToken());
				for (int j=i; j>0; --j)
					if (petra_val[j] > petra_val[j-1] || petra_val[j] == petra_val[j-1] && jan_val[j] < jan_val[j-1]) {
						int temp = petra_val[j];
						petra_val[j] = petra_val[j-1];
						petra_val[j-1] = temp;
						temp = jan_val[j];
						jan_val[j] = jan_val[j-1];
						jan_val[j-1] = temp;
					}
			}
			int [][][] dp = new int[n+1][(n+1)/2+2][2];
			for (int i=1; i<=n; ++i) {
				dp[i][0][0] = dp[i-1][0][0] + petra_val[i-1];
				for (int j=1; j<=(i+jan_start)/2; ++j) {
					int petra_v1 = dp[i-1][j][0] + petra_val[i-1];
					int jan_v1 = dp[i-1][j][1];
					int petra_v2 = dp[i-1][j-1][0];
					int jan_v2 = dp[i-1][j-1][1] + jan_val[i-1];
					if (jan_v1 > jan_v2 || jan_v1 == jan_v2 && petra_v1 > petra_v2) {
						dp[i][j][0] = petra_v1;
						dp[i][j][1] = jan_v1;
					}
					else {
						dp[i][j][0] = petra_v2;
						dp[i][j][1] = jan_v2;
					}
				}
			}
			System.out.println(dp[n][(n+jan_start)/2][0]+" "+dp[n][(n+jan_start)/2][1]);
		}
	}
}
