#include <bits/stdc++.h>
using namespace std;
int solve(string str) {
  int n = str.size();
  stack<char> S;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'A')
      S.push(str[i]);
    else {
      if (S.size() != 0)
        S.pop();
      else
        S.push(str[i]);
    }
  }
  return S.size();
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    string str;
    cin >> str;
    cout << solve(str) << endl;
  }
  return 0;
}
