#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int MOD = 1e9 + 7;
void solve() {
  string s;
  cin >> s;
  int n = int(s.size());
  int b = 0, ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'B')
      ++b;
    else {
      if (b > 0) {
        --b;
      } else {
        ans++;
        ans += b % 2;
        b = 0;
      }
    }
  }
  if (b > 0) ans += b % 2;
  cout << ans << endl;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  for (int tc = 1; tc <= tt; tc++) {
    solve();
  }
}
