#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    string a;
    cin >> a;
    int ans = 0, run = 0;
    for (int i = a.size() - 1; i >= 0; --i) {
      if (a[i] == 'A')
        run--;
      else
        run++;
      if (run < 0) {
        ans++;
        run = 0;
      }
    }
    if (run & 1) ans++;
    cout << ans << '\n';
  }
  return 0;
}
