#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long i, j, k, l, n, p = 0, q, c = 0, d = 0, sum = 0, flag = 0,
                             min1 = 999999999999, max1 = -999999999999, x, y;
    string s;
    cin >> s;
    n = s.size();
    for (i = 0; i < n; i++) {
      if (s[i] == 'B') {
        c++;
        if (d > 0) {
          c--;
          d--;
        }
      } else {
        d++;
      }
    }
    if (c % 2 == 0) {
      c = 0;
    } else
      c = 1;
    cout << c + d << endl;
  }
}
