#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string x;
    cin >> x;
    int cnt[10] = {0};
    for (int i = 0; i < x.length(); ++i) cnt[x[i] - '0']++;
    vector<int> temp;
    int sav = 0;
    for (int i = 0; i <= 9; ++i) {
      if (cnt[i]) temp.push_back(i);
      if ((cnt[i] & 1) && cnt[i] > sav) sav = cnt[i];
    }
    int ans = x.length() - sav;
    for (int i = 0; i < temp.size(); ++i)
      for (int j = 0; j < temp.size(); ++j) {
        int ret = 0, now = 0;
        for (int k = 0; k < x.length(); ++k) {
          if (now & 1) {
            if (x[k] != temp[j] + '0')
              ++ret;
            else
              ++now;
          } else {
            if (x[k] != temp[i] + '0')
              ++ret;
            else
              ++now;
          }
        }
        ans = min(ans, ret + (now & 1));
      }
    cout << ans << endl;
  }
  return 0;
}
