#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
using namespace std;
void test_case() {
string s;
cin >> s;
// ahne?
int on = (int)s.length();
while(true) {
//cout << s << endl;
int n = (int)s.length();
map<char, int> most;
for (int i = 0; i < n; ++i) {
most[s[i]]++;
}
int c = 0;
char mf = '!';
for (auto it = most.begin(); it != most.end(); it++) {
c++;
if (mf == '!') {
mf = it->first;
} else if (most[mf] < it->second) {
mf = it->first;
}
}
if (c <= 1) break;
if (s[0] != mf) {
//cout << "here" << endl;
string ns = "";
bool r = false;
for (int i = 0; i < n; ++i) {
if (s[i+1] == mf && !r) {
i++;
r = true;
continue;
}
ns += s[i];
}
s = ns;
} else {
string ns;
bool r = false;
for (int i = 1; i < n; ++i) {
if (s[i] != mf && !r) {
r = true;
continue;
}
ns += s[i];
}
s = ns;
}
}
cout << on - (int)s.length();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
test_case();
cout << "\n";
//cout << endl;
}
return 0;
}