#include <bits/stdc++.h>
using namespace std;
long long pow(long long p, long long r, long long m) {
  long long ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % m) * (p % m)) % m;
    }
    r = r >> 1;
    p = ((p % m) * (p % m)) % m;
  }
  return ans;
}
string con(string s) {
  for (long long i = 0; i < s.length(); i++) {
    s[i] = toupper(s[i]);
  }
  return s;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    stack<long long> s1;
    for (char i : s) {
      if (!s1.empty() && i == 'B')
        s1.pop();
      else
        s1.push(i);
    }
    cout << s1.size() << '\n';
  }
}
