#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAXN 1000

struct goodie {
	int p, j;
} goodies[MAXN], mem[MAXN+1][MAXN+1];

int compare_petra(const void *_a, const void *_b) {
	struct goodie *a = (struct goodie *)_a;
	struct goodie *b = (struct goodie *)_b;
	if(a->p != b->p)
		return b->p - a->p;
	return a->j - b->j;
}

int n;
char start[100];
bool done[MAXN+1][MAXN+1];

struct goodie rec(int act, int jan) {
	struct goodie tmp;
	int petra = act - jan;
	if(act == n) {
		tmp.p = tmp.j = 0;
		return tmp;
	}
	if(done[act][jan])
		return mem[act][jan];
	done[act][jan] = true;
	if(petra == (n + start[0]/'P') / 2) {
		mem[act][jan] = rec(act+1, jan+1);
		mem[act][jan].j += goodies[act].j;
	} else {
		mem[act][jan] = rec(act+1, jan);
		mem[act][jan].p += goodies[act].p;
		if(jan + start[0]/'P' <= petra) {
			tmp = rec(act+1, jan+1);
			tmp.j += goodies[act].j;
			if(tmp.j > mem[act][jan].j || (tmp.j == mem[act][jan].j && tmp.p > mem[act][jan].p)) {
				mem[act][jan] = tmp;
			}
		}
	}
	return mem[act][jan];
}

void testcase() {
	int i;
	scanf("%d\n", &n);
	scanf("%s\n", start);
	for(i = 0; i < n; i++)
		scanf("%d %d\n", &goodies[i].p, &goodies[i].j);
	qsort(goodies, n, sizeof(struct goodie), compare_petra);
	memset(done, false, sizeof(done));
	printf("%d %d\n", rec(0, 0).p, rec(0, 0).j);
}

int main() {
	int t;
	scanf("%d\n", &t);
	while(t--) testcase();
	return 0;
}
