#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
void solve() {
  string s;
  cin >> s;
  int n = s.length();
  int cnta = 0;
  vector<bool> used(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'A') {
      cnta++;
      used[i] = true;
    } else if (s[i] == 'B' && cnta > 0) {
      cnta--;
      used[i] = true;
    }
  }
  int servono = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'B' && !used[i] && servono == 0) {
      servono++;
    } else if (s[i] == 'B' && !used[i] && servono > 0) {
      servono--;
    }
  }
  cout << cnta + servono << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
