#include <bits/stdc++.h>
using namespace std;
int solve(string s, int x, int y) {
  int t = 0;
  for (auto i : s) {
    if (i - '0' == x) {
      t++;
      swap(x, y);
    }
  }
  if (x != y && t % 2 == 1) --t;
  return t;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        ans = max(ans, solve(s, i, j));
      }
    }
    cout << (int)(s.size()) - ans << endl;
  }
  return 0;
}
