#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int n = s.size();
    int cnt = 0;
    int a = 0, b = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A')
        a++;
      else {
        b++;
        if (a) cnt++, a--;
      }
    }
    b -= cnt;
    cnt += cnt + ((b / 2) * 2);
    cout << n - cnt << endl;
  }
  return 0;
}
