#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    int ans = 0;
    string s;
    int n;
    cin >> s;
    n = s.size();
    vector<int> a;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A')
        a.push_back(1);
      else {
        if (a.empty())
          a.push_back(2);
        else
          a.pop_back();
      }
    }
    ans = a.size();
    cout << ans << endl;
  }
  return 0;
}
