#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
using ll = long long;
int main() {
ll t;
cin >> t;
for (int j = 0; j < t; ++j) {
string s;
cin >> s;
ll a = 0;
ll b = s.size()-1;
ll c = 0;
ll d = 0;
ll siirrot = 0;
if (s[a] == 'B') c++;
if (s[b] == 'A') d++;
if (c && d) siirrot++;
while (a != b) {
//cout << a << " " << b << " " << c << " " << d << endl;
if (c <= d) {
a++;
if (a == b) break;
if (s[a] == 'B') {
c++;
if (c == d) siirrot++;
}
}
else {
b--;
if (b == a) break;
if (s[b] == 'A') {
d++;
if (c == d) siirrot++;
}
}
}
cout << siirrot << endl;
}
}