#include <bits/stdc++.h>
using namespace std;
char s[200100];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    cin >> s;
    int c = 0;
    for (int i = 0; i < strlen(s); i++) {
      if (s[i] == 'A' || c == 0)
        ++c;
      else
        --c;
    }
    cout << c << endl;
  }
  return 0;
}
