#include <bits/stdc++.h>
using namespace std;
void test() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int n = s.length();
  int cnt = 0;
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'A' and b == 0)
      cnt++;
    else if (s[i] == 'A')
      b--;
    else
      b++;
  }
  b = b % 2;
  cnt = cnt + b;
  cout << cnt << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tests = 1;
  cin >> tests;
  while (tests--) {
    test();
  }
}
