#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
  os << "(";
  os << v.first << "," << v.second << ")";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  long long sz = v.size();
  for (long long i = 0; i < sz; ++i) {
    os << v[i];
    if (i != sz - 1) os << ",";
  }
  os << "]\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const list<T>& v) {
  os << "[";
  long long sz = v.size();
  long long i = 0;
  for (auto x : v) {
    os << x;
    if (i != sz - 1) os << ",";
    ++i;
  }
  os << "]\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  T last = *v.rbegin();
  os << "[";
  for (auto it : v) {
    os << it;
    if (it != last) os << ",";
  }
  os << "]\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
  for (auto it : v) {
    os << it.first << " : " << it.second << "\n";
  }
  return os;
}
long long power(long long a, long long b, long long m = 1000000007) {
  long long res = 1;
  while (b) {
    if (b % 2) {
      res = (res % m * a % m) % m;
    }
    b >>= 1;
    a = (a % m * a % m) % m;
  }
  return res;
}
uint64_t toint(const string& s) {
  istringstream src(s);
  uint64_t v;
  src >> v;
  return v;
}
string tostring(uint64_t v) {
  ostringstream src;
  src << v;
  return src.str();
}
long long get_mid(long long a, long long b) { return a + (b - a) / 2; }
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long ceilabyb(long long a, long long b) { return (a + b - 1) / b; }
long long _lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
template <typename T>
void read(vector<T>& v) {
  for (auto& x : v) cin >> x;
}
const long long inf = 1e18;
const long long mxN = 1e5;
void solve() {
  string s;
  cin >> s;
  long long ca = 0, cb = 0;
  long long n = (long long)(s).size();
  for (decltype(n - 1) i = n - 1; i >= 0; i--) {
    (s[i] == 'A' ? ca : cb)++;
    if (s[i] == 'A' and cb > 0) {
      ca--, cb--;
    }
  }
  long long ans = ca + (cb % 2);
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  auto test_cases = [&]() { cin >> t; };
  test_cases();
  for (decltype(1) i = 1; i <= t; i++) {
    solve();
    cout << "\n";
  }
  return 0;
}
