#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define error1(x) cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v) for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;
void solve() {
string s1, s2;
cin >> s1;
map<char, int> mp;
for(auto &i: s1) {
mp[i]++;
}
auto getAns = [&](string &s1, string &s2) -> int {
multiset<pair<char, char>> st;
for(int i = 0; i < s1.size(); i++) {
st.insert({s1[i], s2[i]});
}
int oneOp = 0, moreOp = 0;
map<char, int> mpMoreOp;
for(int i = 0; i < s1.size(); i++) {
char ch1 = s1[i];
char ch2 = s2[i];
auto it1 = st.find({ch1, ch2});
if(ch1 == ch2 or it1 == st.end()) continue;
auto it2 = st.find({ch2, ch1});
if(it2 == st.end()) {
// cout << ch1 << " " << ch2 << endl;
moreOp++;
mpMoreOp[ch1]++;
st.erase(it1);
}
else {
oneOp++;
st.erase(it1);
st.erase(it2);
}
}
// cout << oneOp << " " << moreOp << endl;
return oneOp + (moreOp == 0 ? 0 : moreOp - mpMoreOp.begin()->S);
};
int ans;
if(mp.size() == 1) {
ans = 0;
}
else if(mp.size() == 2) {
char ch1 = mp.begin()->F, ch2 = next(mp.begin())->F;
// AB
s2 = "";
for(int i = 0; i < mp[ch1]; i++) s2 += ch1;
for(int i = 0; i < mp[ch2]; i++) s2 += ch2;
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = getAns(s1, s2);
// BA
s2 = "";
for(int i = 0; i < mp[ch2]; i++) s2 += ch2;
for(int i = 0; i < mp[ch1]; i++) s2 += ch1;
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
}
else {
// ABC
s2 = "";
for(int i = 0; i < mp['A']; i++) s2 += 'A';
for(int i = 0; i < mp['B']; i++) s2 += 'B';
for(int i = 0; i < mp['C']; i++) s2 += 'C';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = getAns(s1, s2);
// ACB
s2 = "";
for(int i = 0; i < mp['A']; i++) s2 += 'A';
for(int i = 0; i < mp['C']; i++) s2 += 'C';
for(int i = 0; i < mp['B']; i++) s2 += 'B';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
// BAC
s2 = "";
for(int i = 0; i < mp['B']; i++) s2 += 'B';
for(int i = 0; i < mp['A']; i++) s2 += 'A';
for(int i = 0; i < mp['C']; i++) s2 += 'C';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
// BCA
s2 = "";
for(int i = 0; i < mp['B']; i++) s2 += 'B';
for(int i = 0; i < mp['C']; i++) s2 += 'C';
for(int i = 0; i < mp['A']; i++) s2 += 'A';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
// CAB
s2 = "";
for(int i = 0; i < mp['C']; i++) s2 += 'C';
for(int i = 0; i < mp['A']; i++) s2 += 'A';
for(int i = 0; i < mp['B']; i++) s2 += 'B';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
// CBA
s2 = "";
for(int i = 0; i < mp['C']; i++) s2 += 'C';
for(int i = 0; i < mp['B']; i++) s2 += 'B';
for(int i = 0; i < mp['A']; i++) s2 += 'A';
// cout << s2 << " => " << getAns(s1, s2) << endl;
ans = min(ans, getAns(s1, s2));
}
cout << ans << endl;
return;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}