#include <bits/stdc++.h>
using namespace std;
inline 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 * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int T, top;
char a[414141], ch;
int main() {
  T = read();
  while (T--) {
    ch = getchar();
    top = 0;
    while (ch != '\n') {
      if (ch == 'A') {
        a[++top] = ch;
      } else {
        if (a[top] == 'A' || a[top] == 'B')
          top--;
        else
          a[++top] = ch;
      }
      ch = getchar();
    }
    printf("%d\n", top);
  }
  return 0;
}
