#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
//#include "stdlib_printing.hh"
using namespace std;
typedef long long LL;
void solve(){
string S; cin >> S;
LL na = 0; LL nb = 0; LL nc = 0;
for(char c : S){
if(c == 'A') na++;
if(c == 'B') nb++;
if(c == 'C') nc++;
}
vector<LL> v = {na,nb,nc};
sort(v.begin(), v.end());
if(v[2] >= v[0] + v[1]) cout << S.size() - (v[2] - v[0] - v[1]) << "\n";
else cout << S.size() - S.size() % 2 << "\n";
}
int main(int argc, char** argv){
LL t; cin >> t; while(t--) solve();
}