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.
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('0')

file=1





def solve():



    
    for _ in range(ii()):

        f={}
        for i in range(10):
            f[i]=[]
        s=si()
        n=len(s)
        for i in range(n):
            f[bo(s[i])].append(i)
        ans=0
        x=0
        for i in range(10):
            if(len(f[i])>ans):
                ans=len(f[i])     
        ans1=0
        for i in range(10):
            for j in range(10):
                if i==j:
                    continue
                l1=0
                l2=0
                a=f[i]
                b=f[j]
                cnt=0
                f1=0
                while(l1<len(a) and l2<len(b)):
                    if a[l1]<b[l2]:
                        l1+=1
                        if(f1==0):
                            cnt+=1
                            f1^=1
                    else:
                        l2+=1
                        if(f1):
                            cnt+=1
                            f1^=1
                if (l2<len(b)):
                    if(f1):
                        cnt+=1

                if(cnt>ans1):
                    ans1=cnt
                    x1=i
                    x2=j
        # print(ans1,x1,x2)
        print(n-max(ans,ans1))


        
        
    
                        
    
    
    
    
    
    
    
    
    
        

        



    










        
if __name__ =="__main__":

    if(file):

        if path.exists('input.txt'):
            sys.stdin=open('input.txt', 'r')
            sys.stdout=open('output.txt','w')
        else:
            input=sys.stdin.readline
    solve()