#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
void solve() {
  string s;
  cin >> s;
  int cnt = 0, ans = 0;
  for (int i = s.size() - 1; i >= 0; i--)
    (s[i] == 'B' ? cnt++ : (cnt ? cnt-- : ans++));
  cout << ans + (cnt & 1) << '\n';
  return;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
