#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define vc vector
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define tra(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
template <typename... T> void get(T &...args) { ((cin >> args), ...); }
template <typename... T> void pri(T &&...args) { ((cout << args << " "), ...); }
template <typename... T> void pril(T &&...args) {
((cout << args << " "), ...);
cout << '\n';
}
using ll = long long;
using vi = vc<int>;
using vll = vc<ll>;
using pi = pair<int, int>;
const ll LINF = 1e18;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int N = 1e6 + 1;
void solve() {
string s;
get(s);
int n = sz(s);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == 'B') {
int last_index = -1;
for (int j = i; j < n; ++j) {
if (s[j] == 'A') {
last_index = max(j, last_index);
}
}
if (last_index == -1) {
break;
}
swap(s[i], s[last_index]);
++ans;
}
}
pril(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int TC;
get(TC);
while (TC--) {
solve();
}
}