#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define F first
#define S second
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tst;
cin>>tst;
for (int t=0;t<tst;t++) {
string s;
cin>>s;
int cntA=0;
int cntB=0;
int cntC=0;
int n=s.size();
for (int i=0;i<n;i++) {
if (s[i]=='A') cntA++;
if (s[i]=='B') cntB++;
if (s[i]=='C') cntC++;
}
if (cntB+cntC<cntA) {
cout<<2*(cntB+cntC)<<"\n";
continue;
}
if (cntA+cntC<cntB) {
cout<<2*(cntA+cntC)<<"\n";
continue;
}
if (cntA+cntB<cntC) {
cout<<2*(cntA+cntB)<<"\n";
continue;
}
if (n%2==1) n--;
cout<<n<<"\n";
}
}