Let's call left cyclic shift of some string t_1 t_2 t_3 ... t_{n - 1} t_n as string t_2 t_3 ... t_{n - 1} t_n t_1.

Analogically, let's call right cyclic shift of string t as string t_n t_1 t_2 t_3 ... t_{n - 1}.

Let's say string t is good if its left cyclic shift is equal to its right cyclic shift.

You are given string s which consists of digits 0–9.

What is the minimum number of characters you need to erase from s to make it good?

Input

The first line contains single integer t (1 ≤ t ≤ 1000) — the number of test cases.

Next t lines contains test cases — one per line. The first and only line of each test case contains string s (2 ≤ |s| ≤ 2 ⋅ 10^5). Each character s_i is digit 0–9.

It's guaranteed that the total length of strings doesn't exceed 2 ⋅ 10^5.

Output

For each test case, print the minimum number of characters you need to erase from s to make it good.

Example

Input


3
95831
100120013
252525252525


Output


3
5
0

Note

In the first test case, you can erase any 3 characters, for example, the 1-st, the 3-rd, and the 4-th. You'll get string 51 and it is good.

In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.

In the third test case, the given string s is already good.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const long long int RUN = 32;
void pend(long long int x) {
  cout << x << "\n";
  ;
}
void pr1(long long int x) { cout << x; }
void pr2(long long int x) { cout << x << " "; }
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
char minchar(char a, char b) {
  if (a > b)
    return b;
  else
    return a;
}
const long long int N = 2e5 + 5;
const long long int K = 100000000;
bool isPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
void reverseStr(string& str, long long int n) {
  for (long long int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]);
}
long long int binarySearch(vector<long long int> arr, long long int l,
                           long long int r, long long int x) {
  while (l <= r) {
    long long int m = l + (r - l) / 2;
    if (arr[m] == x) return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    string a;
    cin >> a;
    long long int n[10] = {};
    for (long long int i = 0; i < a.size(); i++) {
      if (a[i] == '0') {
        n[0]++;
      }
      if (a[i] == '1') {
        n[1]++;
      }
      if (a[i] == '2') {
        n[2]++;
      }
      if (a[i] == '3') {
        n[3]++;
      }
      if (a[i] == '4') {
        n[4]++;
      }
      if (a[i] == '5') {
        n[5]++;
      }
      if (a[i] == '6') {
        n[6]++;
      }
      if (a[i] == '7') {
        n[7]++;
      }
      if (a[i] == '8') {
        n[8]++;
      }
      if (a[i] == '9') {
        n[9]++;
      }
    }
    long long int count = 0;
    for (long long int i = 0; i < 10; i++) {
      if (n[i] > 0) {
        count++;
      }
    }
    if (count == 1) {
      cout << "0"
           << "\n";
      ;
      continue;
    }
    long long int ans = 1000000000000000;
    for (long long int i = 0; i < 10; i++) {
      for (long long int j = 0; j < 10; j++) {
        long long int c = 0, flag = 0;
        for (long long int l = 0; l < a.size(); l++) {
          if (flag == 0) {
            if (a[l] == (i + '0')) {
              flag = 1;
              c++;
            }
          } else {
            if (a[l] == (j + '0')) {
              flag = 0;
              c++;
            }
          }
        }
        if (c % 2 != 0) {
          continue;
        }
        ans = min(ans, a.size() - c);
      }
    }
    for (long long int i = 0; i < 10; i++) {
      ans = min(ans, a.size() - n[i]);
    }
    cout << ans << "\n";
    ;
  }
}
