#include <bits/stdc++.h>
using namespace std;
int main() {
  int tt;
  cin >> tt;
  while (tt--) {
    string s;
    cin >> s;
    map<int, int> cnt;
    int mx = 0;
    for (char c : s) {
      cnt[c - '0']++;
      mx = max(mx, cnt[c - '0']);
    }
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        if (b == a) continue;
        int need = 0;
        int cnt = 0;
        for (char c : s) {
          int d = c - '0';
          if (need == 0) {
            if (d == a) {
              cnt++;
              need ^= 1;
            }
          } else if (need == 1) {
            if (d == b) {
              cnt++;
              need ^= 1;
            }
          }
        }
        if (cnt % 2 == 1) cnt--;
        mx = max(mx, cnt);
      }
    }
    cout << s.size() - mx << '\n';
  }
  return 0;
}
