#include <bits/stdc++.h>
using namespace std;
double mod(double a) {
  if (a < 0) return -1 * a;
  return a;
}
bool prime[100000000 + 1];
void SieveOfEratosthenes() {
  long long int n = 100000000;
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1, u, j;
  cin >> t;
  for (u = 1; u <= t; u++) {
    long long int m, n, i, j, a, b, x, y, k, p = 0, q = 0, p1 = INT_MAX, p2 = 0,
                                             p3 = 0, ans = 0, l, r, index = -1;
    string s;
    cin >> s;
    for (i = 0; i <= 9; i++) {
      for (j = 0; j <= 9; j++) {
        char c1 = i + '0';
        char c2 = j + '0';
        p = 0;
        for (k = 0; k < s.size(); k++) {
          if (p % 2 == 0) {
            if (s[k] == c1) p++;
          } else {
            if (s[k] == c2) p++;
          }
        }
        if (i != j && p % 2 != 0) p--;
        p2 = max(p, p2);
      }
    }
    cout << s.size() - p2 << endl;
  }
}
