#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
  int t, i, num, ans;
  cin >> t;
  while (t--) {
    cin >> s;
    num = ans = 0;
    for (i = s.size() - 1; i >= (0); i--)
      if (s[i] == 'B')
        num++;
      else if (num)
        num--;
      else
        ans++;
    cout << ans + (num & 1) << endl;
  }
  return 0;
}
