#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int tt;
  cin >> tt;
  while (tt--) {
    string s;
    cin >> s;
    int n = s.length();
    auto match = [&](string c) {
      int now = 0, res = 0;
      for (auto i : s) {
        if (i == c[now]) {
          res++;
          now ^= 1;
        }
      }
      return res;
    };
    int ans = 0;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        string c = {char(i + '0'), char(j + '0')};
        int m = match(c);
        if (i != j) {
          m -= m % 2;
        }
        ans = max(ans, m);
      }
    }
    cout << n - ans << '\n';
  }
  return 0;
}
