#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const long long int INF = 1e18;
const long double pi = 3.14159265359;
template <class T>
long long int maxpower(T a, T b) {
  long long int ans = 0;
  while (a > 0 && a % b == 0) {
    ans++;
    a /= b;
  }
  return ans;
}
template <class T>
T mceil(T a, T b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
template <class T>
istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a) cin >> i;
  return in;
}
template <class T>
ostream &operator<<(ostream &ostream, const vector<T> &c) {
  for (auto &it : c) cout << it << " ";
  return ostream;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int ans = 2;
    int n = (long long int)(s).size();
    for (char i = '0'; i <= '9'; i++) {
      for (char j = '0'; j <= '9'; j++) {
        int cnt = 0;
        for (int x = 0; x < n; x++) {
          if (cnt & 1) {
            if (s[x] == j) {
              cnt++;
            }
          } else if (!(cnt & 1)) {
            if (s[x] == i) {
              cnt++;
            }
          }
        }
        if (i != j) {
          if (cnt & 1) {
            cnt--;
          }
        }
        ans = max(ans, cnt);
      }
    }
    cout << n - ans << "\n";
  }
}
