#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int T, n;
char str[N];
vector<pair<int, int> > v;
int main() {
  for (cin >> T; T--;) {
    v.clear();
    scanf("%s", str + 1);
    n = strlen(str + 1);
    for (int i = 1; i <= n; i++) {
      int j = i;
      while (j + 1 <= n && str[j + 1] == str[j]) j++;
      v.push_back({(str[i] == 'A'), j - i + 1});
      i = j;
    }
    int res = n;
    for (int i = v.size() - 1; i >= 0; i--) {
      if (v[i].first == 1) {
        if (i + 1 < v.size() && v[i + 1].second) {
          int minv = min(v[i + 1].second, v[i].second);
          res -= minv * 2;
          v[i + 1].second -= minv, v[i].second -= minv;
          if (v[i].second == 0) {
            if (i >= 1) {
              v[i - 1].second += v[i + 1].second;
              v[i + 1].second = 0;
            }
          }
        }
      } else {
        continue;
      }
    }
    for (int i = 0; i < v.size(); i++) {
      if (v[i].first == 0) {
        if (v[i].second & 1) {
          res -= v[i].second - 1;
        } else {
          res -= v[i].second;
        }
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
