#include <bits/stdc++.h>
long long ans[800005];
int n, t, k, m, len;
long long read() {
  register char ch = getchar();
  register long long ret = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
char s[800005];
int c[2];
int main() {
  int t = read();
  while (t--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    int i = 1, j = 1;
    c[1] = c[0] = 0;
    int ans = 0;
    while (i <= n) {
      j = i;
      while (j <= n && s[i] == s[j]) ++j;
      if (s[i] == 'A') {
        c[0] += j - i;
      } else {
        c[1] += j - i;
        if (c[1] <= c[0]) {
          c[0] -= c[1];
          ans += c[1];
          c[1] = 0;
        } else {
          if ((c[1] + c[0]) & 1) {
            ans += (c[0] + c[1]) / 2;
            c[0] = 1;
            c[1] = 0;
          } else {
            ans += (c[0] + c[1]) / 2;
            c[0] = 0;
            c[1] = 0;
          }
        }
      }
      i = j;
    }
    printf("%d\n", n - ans * 2);
  }
  return 0;
}
