#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int ans = 1000000000;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        int comp = 0;
        int next = i;
        for (int k = 0; k < s.length(); k += 1) {
          if (s[k] - '0' != next)
            comp += 1;
          else {
            if (next == i) {
              next = j;
            } else {
              next = i;
            }
          }
        }
        if (i != j && (s.length() - comp) % 2 == 1) {
          comp++;
        }
        ans = min(ans, comp);
      }
    }
    cout << ans << endl;
  }
}
