#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int max_count = 0;
    for (char ch = '0'; ch <= '9'; ch++) {
      int c1 = 0;
      for (char ch1 = '0'; ch1 <= '9'; ch1++) {
        int f = -1, c2 = 0;
        for (int i = 0; i < s.size(); i++) {
          if (s[i] == ch && f == -1) {
            c2++;
            f = 1;
          } else if (s[i] == ch1 && f == 1) {
            c2++;
            f = -1;
          }
        }
        if (c2 % 2 != 0) c2--;
        max_count = max(max_count, c2);
      }
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == ch) c1++;
      }
      max_count = max(c1, max_count);
    }
    int res = s.size() - max_count;
    cout << res << endl;
  }
  return 0;
}
