#include <bits/stdc++.h>
using namespace std;
const int nx = 2e5 + 5;
char mp[105][2], s[nx];
int t, n;
int cal(char a, char b) {
  int re = 0;
  if (a == b) {
    for (int i = 0; i < n; ++i) re += s[i] == a;
    return re;
  }
  bool f = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == a && !f) ++re, f = 1;
    if (s[i] == b && f) ++re, f = 0;
  }
  return re / 2 * 2;
}
int main() {
  int cnt = 0;
  for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j) mp[cnt][0] = '0' + i, mp[cnt++][1] = '0' + j;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s);
    n = strlen(s);
    int ans = 0;
    for (int i = 0; i < cnt; ++i) ans = max(ans, cal(mp[i][0], mp[i][1]));
    printf("%d\n", n - ans);
  }
}
