#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
string s;
getline(cin, s);
for (int i = 0; i < t; ++i){
int acnt = 0;
int bcnt = 0;
int swp1 = 0;
int swp2 = 0;
getline(cin, s);
for (int i = 0; i < s.size(); ++i){
if(s[i] == 'A'){
++acnt;
}else{
++bcnt;
}
}
for (int i = 0; i < acnt; ++i){
if(s[i] == 'B'){
++swp1;
}
}
for (int i = acnt; i < s.size(); ++i){
if(s[i] == 'A'){
++swp2;
}
}
int tot = (swp2+swp1)/2;
if(swp2 > swp1){
tot += swp2-swp1;
}
if(swp1 > swp2){
tot += swp1-swp2;
}
cout << tot << '\n';
}
flush(std::cout);
}