#include <bits/stdc++.h>
using namespace std;
int ok[100008];
char a[100008];
int t;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%s", a + 1);
    int len = strlen(a + 1);
    int maxx = 0, ans = len;
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        int num = 0, sum = 0;
        for (int k = 1; k <= len; k++) {
          if (num % 2 == 0) {
            if (a[k] - '0' != i)
              sum++;
            else
              num++;
          } else {
            if (a[k] - '0' != j)
              sum++;
            else
              num++;
          }
        }
        if (i != j && num % 2 == 1) sum++;
        ans = min(ans, sum);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
