#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    int ans = INT_MAX;
    string s;
    cin >> s;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        int x = s.length(), is = 0, cnt = 0;
        for (int k = 0; k < x; k++) {
          if (is == 0 and (s[k] - '0') == i) {
            cnt++;
            is = 1;
          } else if (is == 1 and (s[k] - '0') == j) {
            cnt++;
            is = 0;
          }
        }
        if (i != j and cnt % 2 == 1) cnt--;
        if (cnt != 0) ans = min(x - cnt, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
