// NWERC'10 solution for problem 'freegoodies'
// Jaap Eldering

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

typedef vector<int> VI;
typedef vector< VI> VVI;

typedef pair<int,int> value; // value: Jan, Petra

typedef vector<value>   Vval;
typedef vector<Vval>   VVval;
typedef vector<VVval> VVVval;

const int infty = 999999999;

int petracmp(value a, value b)
{
	if ( a.second!=b.second ) return a.second>b.second;
	return a.first<b.first;
}

value operator +(value a, value b)
{
	return value(a.first+b.first,a.second+b.second);
}

int main()
{
	int run, nruns;

	cin >> nruns;

	for(run=1; run<=nruns; run++) {
		int n, njan, npet, janstart;
		cin >> n;

		string start;
		cin >> start;

		njan = n/2;
		janstart = 0;
		if ( start=="Jan" ) {
			janstart = 1;
			if ( n%2 ) njan++;
		}
		npet = n - njan;

		Vval values(n);
		for(int i=0; i<n; i++) cin >> values[i].second >> values[i].first;

		sort(values.begin(),values.end(),petracmp);

		VVval best(n+1,Vval(njan+1,value(-infty,-infty))); // #goodies, #jan
		best[0][0] = value(0,0);

		for(int i=0; i<n; i++) {
			for(int j=0; j<=njan; j++) {

				value forpet = best[i][j]+value(0,values[i].second);
				value forjan = best[i][j]+value(values[i].first,0);

				if ( forpet>best[i+1][j] ) best[i+1][j] = forpet;

				if ( j<njan && (j+1<=(i+1+janstart)/2 || i-j>=npet) &&
				     forjan>best[i+1][j+1] ) best[i+1][j+1] = forjan;
			}
		}

		cout << best[n][njan].second << " "
			 << best[n][njan].first << endl;
	}

	return 0;
}
