#include <bits/stdc++.h>
int n;
char s[200005];
void Solve() {
  scanf("%s", s);
  n = strlen(s);
  int cnt = 0;
  int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == 'B') {
      ++cnt;
    } else {
      if (cnt)
        --cnt;
      else
        ++ans;
    }
  }
  ans += cnt & 1;
  printf("%d\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) Solve();
  return 0;
}
