#include <bits/stdc++.h>
using namespace std;
string input;
int tmp[300005];
int new_tmp[300005];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> input;
    int top = 0;
    for (char i : input) {
      if (i == 'B') {
        if (tmp[top] == 'A') {
          top--;
        } else
          tmp[++top] = i;
      } else
        tmp[++top] = i;
    }
    int new_top = 0;
    for (int i = 1; i <= top; i++) {
      if (tmp[i] == 'B') {
        if (new_tmp[new_top] == 'B')
          new_top--;
        else
          new_tmp[++new_top] = 'B';
      } else
        new_tmp[++new_top] = 'A';
    }
    cout << new_top << endl;
  }
  return 0;
}
