#include <bits/stdc++.h>
using namespace std;
const long long N = 500 * 1000 + 9;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
void HakunaMatata() {
  string s;
  cin >> s;
  long long n = (long long)s.size();
  long long mx = 0;
  for (char a = '0'; a <= '9'; a++) {
    for (char b = '0'; b <= '9'; b++) {
      long long len = 0;
      if (a != b) {
        string t = "";
        t.push_back(a);
        t.push_back(b);
        long long j = 0;
        for (long long i = 0; i < n; i++) {
          if (s[i] == t[j]) {
            if (j == 1) {
              len = len + 2;
              j = 0;
            } else {
              j++;
            }
          }
        }
      } else {
        for (long long i = 0; i < n; i++)
          if (s[i] == a) len++;
      }
      mx = max(mx, len);
    }
  }
  cout << n - mx << '\n';
  return;
}
signed main() {
  long long ___T;
  scanf("%lld", &___T);
  for (long long cs = 1; cs <= ___T; cs++) HakunaMatata();
  return 0;
}
