#include <bits/stdc++.h>
using namespace std;
void scan_arr1(long long int arr[], long long int n, long long int start = 0) {
  for (long long int i = start; i < n + start; i++) cin >> arr[i];
}
void scan_arr2(long long int arr[1010][1010], long long int n, long long int m,
               long long int start = 0) {
  for (long long int i = start; i < n + start; i++)
    for (long long int j = start; j < m + start; j++) cin >> arr[i][j];
}
void print_arr1(long long int arr[], long long int n, long long int start = 0) {
  for (long long int i = start; i < n + start; i++) cout << arr[i] << " ";
  cout << "\n";
}
void print_arr2(long long int arr[1010][1010], long long int n, long long int m,
                long long int start = 0) {
  for (long long int i = start; i < n + start; i++) {
    for (long long int j = start; j < m + start; j++) cout << arr[i][j] << " ";
    cout << "\n";
  }
}
int main() {
  long long int t, n, i, j, k;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    n = s.length();
    long long int res = -1, cnt = 0;
    for (i = 0; i < 10; i++)
      for (j = 0; j < 10; j++) {
        bool f = 0;
        cnt = 0;
        for (k = 0; k < n; k++) {
          if (f == 0 && (s[k] - '0') == i) {
            cnt++;
            f = 1;
          } else if (f == 1 && (s[k] - '0') == j) {
            cnt++;
            f = 0;
          }
        }
        if (cnt % 2 && i != j) cnt--;
        res = max(res, cnt);
      }
    n -= res;
    cout << n << "\n";
  }
}
