#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int mod = 1e9 + 7;
string s;
int n, t;
int calc(int x, int y) {
  int val = 0;
  for (int i = 0; i < s.size(); i++) {
    int digit = s[i] - '0';
    if (digit == x) val++, swap(x, y);
  }
  return val;
}
int even() {
  int val = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      int temp = calc(i, j);
      if (temp % 2 == 0) val = max(val, temp);
      if (temp % 2 && i == j) val = max(val, temp);
    }
  }
  return val;
}
int main() {
  cin >> t;
  int ans;
  while (t--) {
    cin >> s;
    cout << s.size() - even() << '\n';
  }
  return 0;
}
