#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vc = vector<T>;
using pii = pair<long long, long long>;
void xmax(long long& a, long long b) { a = max(a, b); }
void xmin(long long& a, long long b) { a = min(a, b); }
void print(vc<long long>& a, string second) {
  cerr << second << " : ";
  for (long long i = 0; i < a.size(); i++) {
    cerr << a[i] << " ";
  }
  cerr << endl;
}
void do_test() {
  string second;
  cin >> second;
  long long anz = 0;
  long long hat = 0;
  for (long long i = 0; i < second.size(); i++) {
    if (second[i] == 'A')
      anz++;
    else {
      if (anz == 0) {
        hat++;
      } else {
        anz--;
      }
    }
  }
  cout << anz + (hat % 2) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) do_test();
}
