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