#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        int a = i, b = j;
        int ct = 0;
        for (auto el : s) {
          if (el - '0' == a) {
            ct++;
            swap(a, b);
          }
        }
        if (a != b && ct % 2) ct--;
        ans = max(ans, ct);
      }
    }
    cout << s.length() - ans << "\n";
  }
  return 0;
}
