#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
long long compare(pair<long long, long long> &a,
                  pair<long long, long long> &b) {
  return a.first > b.first;
}
long long compare2(pair<long long, long long> &a,
                   pair<long long, long long> &b) {
  return a.second < b.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T;
  cin >> T;
  while (T--) {
    long long ans = 0;
    string s;
    cin >> s;
    for (long long i = 0; i < 10; i++) {
      for (long long j = 0; j < 10; j++) {
        char ch1 = '0' + i;
        char ch2 = '0' + j;
        char cur = ch1;
        long long tmp = 0;
        for (long long k = 0; k < s.length(); k++) {
          if (s[k] == cur) {
            tmp++;
            if (cur == ch1)
              cur = ch2;
            else
              cur = ch1;
          }
        }
        if (ch1 == ch2) {
          ans = max(ans, tmp);
        } else {
          if (tmp % 2 != 0) tmp--;
          ans = max(ans, tmp);
        }
      }
    }
    ans = (s.length()) - ans;
    cout << ans << endl;
  }
}
