#include <bits/stdc++.h>
using namespace std;
string s;
int n;
long long ans;
void solve() {
  cin >> s;
  n = s.size();
  reverse(s.begin(), s.end());
  ans = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'B')
      cnt++;
    else if (cnt)
      cnt--;
    else
      ans++;
  }
  ans += (cnt & 1);
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
