#include <bits/stdc++.h>
using namespace std;
const long long N = 100001;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long ans = 0;
    long long n = s.length();
    for (long long i = 0; i < 10; i++) {
      for (long long j = 0; j < 10; j++) {
        long long c = 0;
        long long cur = 0;
        for (long long k = 0; k < n; k++) {
          if (c % 2 == 0) {
            if (s[k] - '0' == i) {
              cur++;
              c++;
            }
          } else {
            if (s[k] - '0' == j) {
              cur++;
              c++;
            }
          }
        }
        if (cur % 2 == 0 || (cur % 2 == 1 && i == j)) ans = max(ans, cur);
      }
    }
    cout << (n - ans) << endl;
  }
}
