#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.size();
    vector<int> taken(n, 0);
    deque<int> a;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A') {
        a.push_back(i);
      } else {
        if (!a.empty()) {
          int now = a.front();
          a.pop_front();
          taken[now] = taken[i] = 1;
        }
      }
    }
    string left = "";
    for (int i = 0; i < n; i++)
      if (!taken[i]) left += s[i];
    int ans = count(left.begin(), left.end(), 'A') +
              (count(left.begin(), left.end(), 'B') % 2 ? 1 : 0);
    cout << ans << endl;
  }
}
