#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:255999999")
using namespace std;
const long long mod = 1e+9 + 7;
const long long powerer = 271, inver = 450184505;
bool CheckCircle(string& s, char belt) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != belt && s[i] != '-') return 0;
  }
  return 1;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  int t, n;
  string s;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> s;
    int Bcounter = 0, Acounter = 0, ans = s.size();
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'A') {
        Acounter++;
      } else if (Acounter != 0) {
        Acounter--;
        ans -= 2;
      } else if (Bcounter != 0 && Acounter == 0) {
        Bcounter--;
        ans -= 2;
      } else
        Bcounter++;
    }
    cout << ans << '\n';
  }
  return 0;
}
