#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const int N = 1e5 + 5;
int T, n, m, k, q;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    n = s.size();
    int mx = 0;
    for (int x = 0; x < 10; x++) {
      for (int y = 0; y < 10; y++) {
        int mark = 0;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
          if (s[i] - '0' == x && mark == 0)
            cnt++, mark = 1;
          else if (s[i] - '0' == y && mark == 1)
            cnt++, mark = 0;
        }
        if (cnt % 2 == 1 && x != y) cnt--;
        mx = max(mx, cnt);
      }
    }
    cout << n - mx << "\n";
  }
}
