#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n, countA, countB;
  string s;
  cin >> t;
  while (t--) {
    countA = 0;
    countB = 0;
    cin >> s;
    n = s.size();
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A') countA++;
      if (s[i] == 'B') {
        countB++;
        if (countA != 0) {
          countA--;
          countB--;
        }
      }
    }
    if (countB % 2 == 0)
      countB = 0;
    else
      countB = 1;
    cout << countB + countA << endl;
  }
  return 0;
}
