#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline int read() {
  int x = 0, f = 1;
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 2e5 + 10;
int T, n, cnt, ans;
char S[N];
int main() {
  T = read();
  while (T--) {
    scanf("%s", S);
    n = strlen(S);
    cnt = ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (S[i] == 'B') cnt++;
      if (S[i] == 'A') {
        if (!cnt)
          ans++;
        else
          cnt--;
      }
    }
    ans += (cnt & 1);
    printf("%d\n", ans);
  }
  return 0;
}
