#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int l = s.size(), i;
    vector<char> a;
    for (i = 0; i < l; i++) {
      if (s[i] == 'A')
        a.push_back(s[i]);
      else {
        if (a.empty())
          a.push_back(s[i]);
        else
          a.pop_back();
      }
    }
    cout << a.size() << endl;
  }
}
