#include <bits/stdc++.h>
using namespace std;
int t, n, a, b, num;
char d;
bool c (int x, int y, int ii, int jj) {
for (int i = 1; i <= n; i++)
return true;
}
void dfs (int x, int y, int tt) {
if (tt == n) {
n ++;
}
if (tt > n) {
n = tt;
n = 1;
}
for (int i = x + 1; i <= n; i++) {
for (int j = y + 1; j <= n; j++) {
if (c(x, y, i, j) == true) {
dfs (i, j, tt + 1);
}
}
}
}
int main () {
freopen("silly.in", "r", stdin);
freopen("silly.out", "w", stdout);
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n;
a = 0;
b = 0;
for (int j = n; j >= 1; j--) {
cin >> num >> d;
if (d == 'B') {
a += num;
}
else {
b += num;
}
}
if (a == 0 || b == 0) cout << max (a, b) << endl;
else if (a == b) cout << 2 << endl;
else if (a > b) cout << a / b << endl;
else if (b > a) cout << b / a << endl;
}
return 0;
}
/*
----*----
---***---
--*****--
-*******-
----|----
3 2 -4 2 3
5 5
*/