#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve() {
  string s;
  cin >> s;
  int z = s.size();
  ll ans = z;
  ll res = 0;
  ll cnta = 0, cntb = 0;
  for (ll i = 0; i < z; i++) {
    if (s[i] == 'A') {
      cnta++;
    } else {
      cntb++;
      if (cnta > 0 && cntb > 0) {
        cnta--;
        cntb--;
        ans -= 2;
      }
      if (cntb >= 2) {
        cntb -= 2;
        ans -= 2;
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int q = 1;
  cin >> q;
  while (q--) {
    solve();
  }
}
