#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INF = 1e18;
const int MX = 100001;
long long a[301201], vis[201201];
void solve() {
  int n, k;
  bool ok = true;
  string s;
  vector<char> v;
  cin >> s;
  n = s.size();
  v.push_back(s[0]);
  for (int i = 1; i < n; i++) {
    if (v.empty())
      v.push_back(s[i]);
    else if (v.back() == 'B' && s[i] == 'A') {
      v.push_back(s[i]);
    } else if (v.back() == 'A' && s[i] == 'A') {
      v.push_back(s[i]);
    } else {
      v.pop_back();
    }
  }
  cout << v.size() << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
