#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  for (int f = 0; f < t; f++) {
    string s;
    cin >> s;
    long long int n = s.length();
    long long int count = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A') {
        count++;
      }
      if (s[i] == 'B') {
        if (count > 0) {
          count--;
        } else {
          count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
