#include <bits/stdc++.h>
using namespace std;
int n, k, z;
void solve() {
  string s;
  cin >> s;
  n = s.size();
  map<char, int> cnt;
  for (auto c : s) cnt[c]++;
  int mx = -1, mx1 = -1;
  for (auto x : cnt) mx1 = max(mx1, x.second);
  if (mx1 == n) {
    cout << "0\n";
    return;
  }
  int mn = n - mx1;
  for (char c = '0'; c <= '9'; c++) {
    for (char z = '0'; z <= '9'; z++) {
      char curr = c, next = z;
      int count = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == curr) {
          count++;
          swap(curr, next);
        }
      }
      if (count & 1) count--;
      mn = min(mn, n - count);
    }
  }
  cout << mn << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
