#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long power(long long a, long long b) {
  long long ans = 1;
  if (b == 0) return 1;
  if (b % 2 == 0) {
    ans = (ans * power((a * a) % mod, b / 2)) % mod;
  } else {
    ans = (ans * a) % mod;
    ans = (ans * power((a * a) % mod, b / 2)) % mod;
  }
  return ans;
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long ans = 0;
    for (long long i = 0; i < 10; i++) {
      for (long long j = 0; j < 10; j++) {
        long long ct = 0;
        for (long long k = 0; k < s.length(); k++) {
          if (ct % 2 == 0) {
            if (s[k] - '0' == i) ct++;
          } else {
            if (s[k] - '0' == j) ct++;
          }
        }
        if (i == j)
          ans = max(ans, ct);
        else
          ans = max(ans, 2 * (ct / 2));
      }
    }
    cout << s.length() - ans << endl;
  }
}
