#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
long long i, j;
void solve() {
  string s;
  cin >> s;
  long long n = s.size();
  vector<long long> a;
  for (i = 0; i < n; i++) {
    long long sz = 0;
    for (j = i; j < n; j++)
      if (s[j] != s[i]) break;
    sz = j - i;
    a.push_back(sz);
    i = j - 1;
  }
  long long ans = 0, extra = 0;
  if (s[0] == 'B') {
    i = 1;
    extra = a[0];
  } else
    i = 0;
  long long passon = 0;
  for (i; (i + 1) < a.size(); i += 2) {
    long long Asz = a[i] + passon, Bsz = a[i + 1];
    if (Asz >= Bsz) {
      ans += 2 * Bsz;
      passon = Asz - Bsz;
    } else {
      ans += 2 * Asz;
      if ((Bsz - Asz) % 2) {
        ans += (Bsz - Asz - 1);
        if (extra % 2) {
          extra++;
          passon = 0;
        } else
          passon = 1;
      } else {
        ans += Bsz - Asz;
        passon = 0;
      }
    }
  }
  ans += (extra / 2) * 2;
  cout << n - ans;
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
