#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
char s[200010];
signed main() {
  int t = read();
  while (t--) {
    scanf("%s", s + 1);
    int an = 0, ans = strlen(s + 1), ber = 0;
    for (int i = 1, T = strlen(s + 1); i <= T; i++)
      if (s[i] == 'A')
        ++an;
      else {
        if (!an)
          ++ber;
        else
          --an, ans -= 2;
      }
    ans -= ber - ber % 2;
    printf("%d\n", ans);
  }
  return 0;
}
