#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    string S;
    cin >> S;
    int res = 0, ans = 0;
    for (char a = '0'; a <= '9'; a++)
      for (char b = '0'; b <= '9'; b++) {
        res = 0;
        char c = a;
        for (auto s : S) {
          if (s == c) {
            res++;
            c = a + b - c;
          }
        }
        if (a != b) res = 2 * (res / 2);
        ans = (ans > res) ? ans : res;
      }
    cout << S.size() - ans << endl;
  }
  return 0;
}
