#include <bits/stdc++.h>
using namespace std;
void rf() {}
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void solve() {
  int k = 0;
  string s;
  cin >> s;
  k = 0;
  for (int i = 0; i < s.size(); i++) {
    if (k && s[i] == 'B') {
      k--;
    } else {
      k++;
    }
  }
  cout << k << endl;
}
int main() {
  fast();
  rf();
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
