#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n;
    string s;
    cin >> s;
    n = s.length();
    stack<char> ss;
    for (int i = 0; i < n; i++) {
      if (ss.empty()) {
        ss.push(s[i]);
      } else if (s[i] == 'B') {
        ss.pop();
      } else {
        ss.push(s[i]);
      }
    }
    cout << ss.size() << endl;
  }
}
