#include <bits/stdc++.h>
using namespace std;
long long int mi(long long int n, long long int m) {
  long long int pw = n % m;
  long long int ex = m - 2;
  long long int ans = 1;
  while (ex) {
    if (ex & 1) ans = ans * pw % m;
    pw = pw * pw % m;
    ex >>= 1;
  }
  return ans % 1000000007;
}
long long int pw(long long int a, long long int n) {
  long long int pw = a;
  long long int ex = n;
  long long int ans = 1;
  while (ex) {
    if (ex & 1) ans = ans * pw;
    pw = pw * pw;
    ex >>= 1;
  }
  return ans;
}
long long int pwm(long long int a, long long int n) {
  long long int pw = a % 1000000007;
  long long int ex = n;
  long long int ans = 1;
  while (ex) {
    if (ex & 1) ans = ans * pw % 1000000007;
    pw = pw * pw % 1000000007;
    ex >>= 1;
  }
  return ans % 1000000007;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) is >> t;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\n';
  return os;
}
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
long long int max(long long int a, long long int b) { return a > b ? a : b; }
long long int min(long long int a, long long int b) { return a < b ? a : b; }
long long int Abs(long long int x) { return x > 0 ? x : (-x); }
long long int Ceil(long long int a, long long int b) {
  return a / b + (a % b != 0);
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int X[] = {2, 1, -1, -2, -2, -1, 1, 2};
int Y[] = {1, 2, 2, 1, -1, -2, -2, -1};
struct comp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    if (a.first > b.first) return true;
    if (a.first < b.first) return false;
    if (a.second < b.second) return true;
    return false;
  }
};
void solve() {
  string s;
  cin >> s;
  int ans = 0;
  int n = s.length();
  map<string, int> maa;
  int maz = INT_MAX;
  for (char ch = '0'; ch <= '9'; ch++) {
    int aq = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] != ch) aq++;
    }
    maz = min(aq, maz);
  }
  for (char i = '0'; i <= '9'; ++i) {
    for (char j = '0'; j <= '9'; ++j) {
      int l = 0;
      char g = i;
      for (int k = 0; k < n; ++k) {
        if (s[k] == g) {
          l++;
          if (g == i)
            g = j;
          else
            g = i;
        }
      }
      if (i != j and l % 2 == 1) {
        continue;
      }
      ans = max(ans, l);
    }
  }
  cout << n - (ans);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
    ;
  };
}
