// Author: Lukas Polacek
// Date: October 18th, 2010
// Algorithm: DP
// Complexity: O(N^2)

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <stack>
#include <cstdio>
#include <cassert>
using namespace std;

#define rep(i,a,b) for(__typeof(b) i=a; i<(b); ++i)

typedef pair<int, int> pii;
typedef vector<int> vi;
int main()
{
    int t; scanf("%d", &t);
    while (t--)
    {
        int n; scanf("%d", &n);
        string s; cin >> s;
        vector<pii> a(n);
        rep(i,0,n)
            scanf("%d %d", &a[i].first, &a[i].second);
        rep(i,0,n) a[i].first *= -1;
        sort(a.begin(), a.end());
        rep(i,0,n) a[i].first *= -1;

        bool jan_starts = s == "Jan";
        int q[] = {n / 2, n / 2};
        if (n % 2) q[!jan_starts]++;
        pii res[q[0] + 1][q[1] + 1];
        rep(i,0,q[0] + 1) rep(j,0,q[1] +1)
            res[i][j].first = res[i][j].second = -123456789;
        res[0][0] = pii(0, 0);

        rep(jan,0,q[0] + 1) rep(petra,0,q[1] + 1) if (jan + petra > 0)
        {
            if (jan - jan_starts > petra) continue;

            pii &cur = res[jan][petra];
            if (petra > 0)
            {
                cur = res[jan][petra - 1];
                cur.second += a[jan + petra - 1].first;
            }
            if (jan > 0)
            {
                pii tmp = res[jan - 1][petra];
                tmp.first += a[jan + petra - 1].second;
                if (tmp > cur)
                    cur = tmp;
            }
        }

        pii &outp = res[q[0]][q[1]];
        cout << outp.second << " " << outp.first << endl;
    }
}
