#include <bits/stdc++.h>
using namespace std;
int t, n, a, b, num[100005], ans, c, f, e;
char d[100005];
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;
ans = 0;
f = 0;
e = 0;
for (int j = 1; j <= n; j++) {
cin >> num[j] >> d[j];
if (d[j] == 'B') {
for (int k = 1; k <= num[j]; k++) {
a++;
}
} else {
for (int k = 1; k <= num[j]; k++) {
b++;
}
}
}
c = a;
a /= __gcd(a, b);
b /= __gcd(c, b);
for (int j = 1; j <= n; j++) {
if (d[j] == 'B') {
for (int k = 1; k <= num[j]; k++) {
f++;
if (f / __gcd(f, e) == a && e / __gcd(f, e) == b) ans ++;
}
} else {
for (int k = 1; k <= num[j]; k++) {
e++;
if (f / __gcd(f, e) == a && e / __gcd(f, e) == b) ans ++;
}
}
}
cout << ans << endl;
}
return 0;
}
/*
----*----
---***---
--*****--
-*******-
----|----
3 2 -4 2 3
5 5
*/