/*
	[NWERC'10] FREE GOODIES
	by: Jan Kuipers
*/

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct compare_function {
  bool operator() (pair<int,int> a, pair<int,int> b) {
		if (a.first != b.first) return a.first > b.first;
		return a.second < b.second;
	}
} compare;

int main () {

	int runs;
	cin>>runs;

	while (runs--) {

		int N;
		string start;
		cin>>N>>start;
		vector<pair<int,int> > val(N);
		for (int i=0; i<N; i++) 
			cin>>val[i].first>>val[i].second;
		sort(val.begin(),val.end(),compare);

		int totalP=0, totalJ=0;
		if (start=="Petra") {
			totalP += val[0].first;
			val.erase(val.begin());
			N--;
		}

		for (int i=0; i<N; i++)
			swap(val[i].first,val[i].second);
					
		multiset<pair<int,int>,compare_function> taken;
		
		for (int i=0; i<N; i++) {
			if (i%2==0) {
				totalJ += val[i].first;
				taken.insert(val[i]);
			}
			else if (!compare(val[i],*taken.rbegin())) {
				totalP += val[i].second;
			}
			else {
				totalP += (--taken.end())->second;
				totalJ -= (--taken.end())->first;
				totalJ += val[i].first;
				taken.erase(--taken.end());
				taken.insert(val[i]);
			}
		}

		cout<<totalP<<" "<<totalJ<<endl;
	}

	return 0;
}
