#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e5 + 5;
string s;
int n;
int f(int x, int y) {
  int z = x;
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == z) {
      res++;
      z = x + y - z;
    }
  }
  if (res & 1) res -= (x != y);
  return res;
}
void solve() {
  cin >> s;
  n = s.size();
  for (char &c : s) c -= '0';
  int res = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      res = max(res, f(i, j));
    }
  }
  cout << n - res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
