#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
  out << '(' << item.first << ", " << item.second << ')';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const auto &item : v) out << item << ' ';
  return out;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  string s;
  for (cin >> t; t; --t) {
    cin >> s;
    reverse(s.begin(), s.end());
    stack<char> stk;
    for (auto ch : s) {
      if (ch == 'A') {
        if (!stk.empty() && stk.top() == 'B')
          stk.pop();
        else
          stk.push(ch);
      } else {
        stk.push(ch);
      }
    }
    int nrb = 0;
    while (!stk.empty() && stk.top() == 'B') stk.pop(), ++nrb;
    cout << stk.size() + (nrb % 2) << '\n';
  }
  return 0;
}
