#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

struct jp {
    int jan;
    int petra;
} goodies[1010];

bool bestpetra(const jp& a, const jp& b)
{
    if (a.petra == b.petra) return a.jan < b.jan;
    return a.petra > b.petra;
}

struct worstjan {
    bool operator()(const jp& a, const jp& b)
    {
        if (a.jan == b.jan) return a.petra < b.petra;
        return a.jan > b.jan;
    }
};

int main()
{
    int t, n;
    cin >> t;

    while (t--) {
        string first;
        cin >> n >> first;
        for (int ii = 0; ii < n; ++ii)
            cin >> goodies[ii].petra >> goodies[ii].jan;

        int petra = 0, jan = 0;
        sort(goodies, goodies + n, bestpetra);
        if (first == "Petra")
            petra += goodies[0].petra;

        priority_queue<jp, vector<jp>, worstjan> q;
        for (int ii = (first == "Petra"); ii < n; ii += 2) {
            q.push(goodies[ii]);
            if (ii+1 < n) {
                q.push(goodies[ii+1]);
                petra += q.top().petra;
                q.pop();
            }
        }

        while (!q.empty()) {
            jan += q.top().jan;
            q.pop();
        }

        cout << petra << " " << jan << endl;
    }

    return 0;
}
