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>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
vector<long long int> v;
vector<long long int> w, ab, bc;
set<long long int> uo;
long long int BIT[500001];
map<long long int, long long int> ww, cc, u;
vector<pair<long long int, long long int> > uu;
map<long long int, vector<long long int> > uuu;
map<pair<long long int, long long int>, long long int> vo;
map<pair<long long int, long long int>, long long int> vu;
map<long long int, pair<long long int, pair<long long int, long long int> > >
    vs;
long long int dxx[] = {-1, 0, 1, 0};
long long int dyy[] = {0, 1, 0, -1};
long long int dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
long long int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch;
  string a, b, c;
  double x, y, d, e, f, g, h;
  long long int n, m, i, j, k, p, q, o, l, s, tt = 1, t, z, aa, r = 0;
  cin >> tt;
  for (long long int mau = 1; mau <= tt; mau++) {
    v.clear();
    w.clear();
    ww.clear();
    uu.clear();
    uo.clear();
    vo.clear();
    vu.clear();
    ab.clear();
    bc.clear();
    cc.clear();
    uuu.clear();
    a.clear();
    b.clear();
    c.clear();
    u.clear();
    cin >> a;
    k = 0;
    for (i = 0; a[i]; i++) {
      u[a[i]]++;
      k = max(k, u[a[i]]);
    }
    c = "0123456789";
    for (i = 0; c[i]; i++) {
      for (j = 0; c[j]; j++) {
        if (i == j) {
          continue;
        }
        l = 0;
        s = 0;
        for (o = 0; o < a.size(); o++) {
          if (s == 0) {
            if (a[o] == c[i]) {
              l++;
              s = 1;
            }
          } else {
            if (a[o] == c[j]) {
              l++;
              s = 0;
            }
          }
        }
        if (s == 1) {
          l--;
        }
        k = max(k, l);
      }
    }
    cout << a.size() - k << "\n";
  }
  return 0;
}
