#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A>
string to_string(A);
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto& x : v) {
    if (!f) r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename A, typename B>
istream& operator>>(istream& input, pair<A, B>& x) {
  input >> x.first >> x.second;
  return input;
}
template <typename A>
istream& operator>>(istream& input, vector<A>& x) {
  for (auto& i : x) input >> i;
  return input;
}
template <typename A>
ostream& operator<<(ostream& output, vector<A>& x) {
  for (auto& i : x) output << i << ' ';
  return output;
}
template <typename A, typename B>
istream& operator<<(istream& output, pair<A, B>& x) {
  output << x.first << ' ' << x.second;
  return output;
}
long long mod = 1000000007;
void solve() {
  long long n, a = 0, b = 0, c = 0, d = 0, x, y, k, l, f1 = 1, f2 = 1, z = 0,
               m = 0, r = 10e16, f;
  string s;
  char ch, ch1;
  cin >> s;
  n = s.length();
  vector<vector<int> > v(10);
  for (char i = '0'; i <= '9'; i++) {
    for (char j = '0'; j <= '9'; j++) {
      f = 0;
      c = 0;
      for (int a = 0; a < n; a++) {
        if (s[a] == i && f == 0) {
          c++;
          f = 1;
        } else if (s[a] == j && f == 1) {
          c++;
          f = 0;
        }
      }
      if (m < c) {
        m = c;
        ch = i;
        ch1 = j;
      }
      if (m == c && i == j) {
        m = c;
        ch = i;
        ch1 = j;
      }
    }
  }
  if (m % 2 != 0 && ch != ch1) --m;
  cout << n - m << "\n";
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1, p;
  cin >> t;
  p = t;
  while (t--) {
    solve();
  }
  return 0;
}
