#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
vector<long long int> v;
vector<long long int> w, ab, bc;
set<long long int> uo;
long long int BIT[500001];
map<long long int, long long int> ww, cc, u;
vector<pair<long long int, long long int> > uu;
map<long long int, vector<long long int> > uuu;
map<pair<long long int, long long int>, long long int> vo;
map<pair<long long int, long long int>, long long int> vu;
map<long long int, pair<long long int, pair<long long int, long long int> > >
    vs;
long long int dxx[] = {-1, 0, 1, 0};
long long int dyy[] = {0, 1, 0, -1};
long long int dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
long long int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch;
  string a, b, c;
  double x, y, d, e, f, g, h;
  long long int n, m, i, j, k, p, q, o, l, s, tt = 1, t, z, aa, r = 0;
  cin >> tt;
  for (long long int mau = 1; mau <= tt; mau++) {
    v.clear();
    w.clear();
    ww.clear();
    uu.clear();
    uo.clear();
    vo.clear();
    vu.clear();
    ab.clear();
    bc.clear();
    cc.clear();
    uuu.clear();
    a.clear();
    b.clear();
    c.clear();
    u.clear();
    cin >> a;
    k = 0;
    for (i = 0; a[i]; i++) {
      u[a[i]]++;
      k = max(k, u[a[i]]);
    }
    c = "0123456789";
    for (i = 0; c[i]; i++) {
      for (j = 0; c[j]; j++) {
        if (i == j) {
          continue;
        }
        l = 0;
        s = 0;
        for (o = 0; o < a.size(); o++) {
          if (s == 0) {
            if (a[o] == c[i]) {
              l++;
              s = 1;
            }
          } else {
            if (a[o] == c[j]) {
              l++;
              s = 0;
            }
          }
        }
        if (s == 1) {
          l--;
        }
        k = max(k, l);
      }
    }
    cout << a.size() - k << "\n";
  }
  return 0;
}
