#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) -> ll {
multiset<pair<char, char>> st1, st2;
for(int i = 0; i < s1.size(); i++) {
st1.insert({s1[i], s2[i]});
st2.insert({s1[i], s2[i]});
}
ll oneOp = 0, moreOp = 0;
map<char, ll> mpMoreOp;
for(auto &[i, j]: st1) {
auto it1 = st2.find({i, j});
if(i == j or it1 == st2.end()) continue;
auto it2 = st2.find({j, i});
if(it2 == st2.end()) {
// cout << i << " " << j << endl;
moreOp++;
mpMoreOp[i]++;
st2.erase(it1);
}
else {
oneOp++;
st2.erase(it1);
st2.erase(it2);
}
}
// cout << oneOp << " " << moreOp << endl;
return oneOp + (moreOp == 0 ? 0 : moreOp - mpMoreOp.begin()->S);
};
ll 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;
}