#include <bits/stdc++.h>
int __gcd(int a, int b) {
  if (b == 0) return a;
  a %= b;
  return __gcd(b, a);
}
using namespace std;
int const LIM = 1e6 + 3;
int const MLOG = 18;
int arr[LIM + 3];
void solve() {
  string s;
  cin >> s;
  int cnt = 0, is = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A')
      cnt++;
    else if (cnt)
      cnt--;
    else
      is++;
  }
  cout << (is % 2) + cnt << endl;
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    solve();
  }
}
