#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int oo = 1e9 + 5;
const int INF = 1e9 + 5;
const long long int ooll = 1e18 + 5;
long long int v[N];
void solve() {
  string s;
  cin >> s;
  int n = s.size(), ans = n - 1;
  for (int odd = 0; odd < 10; ++odd) {
    for (int eve = 0; eve < 10; ++eve) {
      int need = 0, have = 0;
      for (auto it : s) {
        if (need) {
          if (eve == it - '0') {
            have++;
            need ^= 1;
          }
        } else {
          if (odd == it - '0') {
            have++;
            need ^= 1;
          }
        }
      }
      if (have & 1) {
        if (odd != eve) --have;
      }
      ans = min(ans, n - have);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
