#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, l, m, n, a, b, c, d, t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    l = s.size();
    c = 0;
    for (j = 0; j < 10; j++) {
      for (i = 0; i < 10; i++) {
        a = 0;
        m = 0;
        for (k = 0; k < l; k++) {
          if (a == 0 && (int)s[k] - '0' == j) {
            a = 1;
            m++;
          } else if (a == 1 && (int)s[k] - '0' == i) {
            a = 0;
            m++;
          }
        }
        if (i == j) {
          c = max(c, m);
        } else {
          if (m % 2 == 1) m--;
          c = max(c, m);
        }
      }
    }
    cout << l - c << endl;
  }
  return 0;
}
