#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    long long cnt = 0, mx = 2;
    map<char, long long> mp;
    for (auto x : str) {
      mp[x]++;
      mx = max(mx, mp[x]);
    }
    for (char i = '0'; i <= '9'; i++) {
      for (char j = '0'; j <= '9'; j++) {
        cnt = 0;
        char a = i, b = j;
        for (long long k = 0; str[k]; k++) {
          if (cnt % 2 == 0) {
            if (str[k] == a) cnt++;
          } else {
            if (str[k] == b) cnt++;
          }
        }
        if (cnt % 2) cnt--;
        mx = max(mx, cnt);
      }
    }
    cout << str.size() - mx << endl;
  }
}
