#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  long long t, n, l, r;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    n = s.length();
    long long f[15] = {0};
    long long mx = 0;
    for (auto c : s) {
      f[c - '0']++;
    }
    for (long long i = 0; i < 10; i++) mx = max(mx, f[i]);
    for (long long i = 0; i < 10; i++) {
      string tmp = "";
      long long len = 0;
      char curr = '0' + i;
      char prev;
      for (long long j = 0; j < 10; j++) {
        len = 0;
        if (i == j) continue;
        prev = '0' + j;
        for (char ch : s) {
          if (len & 1) {
            if (ch == prev) len++;
          } else {
            if (ch == curr) len++;
          }
        }
        if (len % 2 == 0) mx = max(mx, len);
      }
    }
    cout << n - mx << "\n";
  }
  return 0;
}
