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.
import java.util.*;
import java.io.*;  

public class Solution{
    public static class pair{
        int x;
        int y;
        int z;
    }
    public static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
        public String next() {
			while (st == null || !st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}public double nextDouble() {
			return Double.parseDouble(next());
		}
    }
    public static void main(String[] args) throws IOException{
        FastScanner fs=new FastScanner();
		int t=fs.nextInt();
		PrintWriter out=new PrintWriter(System.out);
		while(t-->0){
		    String s=fs.next();
		    int[] arr=new int[10];
		    for(int i=0;i<s.length();i++){
		        arr[s.charAt(i)-'0']++;
		    }
		    int max=Integer.MIN_VALUE;
		    int smax=Integer.MIN_VALUE;
		    int min=-1;
		    int smin=-1;
		    for(int i=0;i<arr.length;i++){
		        if(arr[i]>=max){
		            smax=max;
		            smin=min;
		            max=arr[i];
		            min=i;
		        }
		    }
		    int ans=s.length()-max;
		  //  if(smax!=-1){
		  //      ans=Math.min(ans,fut(s,min,smin));
		  //      ans=Math.min(ans,fut(s,smin,min));
		  //  }
		  for(int i=0;i<=9;i++){
		      for(int j=0;j<=9;j++){
		          int v=fut(s,i,j);
		          ans=Math.min(ans,v);
		      }
		  }
		    System.out.println(ans);
		}
        out.close();
    }
    public static int fut(String s,int fi,int sec){
        // System.out.println(fi+" "+sec);
        boolean f=true;
        int d=0;
        int t=0;
        for(int i=0;i<s.length();i++){
            if(f&&((int)(s.charAt(i)-'0')==fi)){
                f=!f;
                t++;
            }else if(!f&&((int)(s.charAt(i)-'0')==sec)){
                f=!f;
                t++;
            }else{
                d++;
            }
        }
        if(t%2!=0){
            d++;
        }
        return d;
    }
    public static String tree(int n){
        StringBuffer sb=new StringBuffer();
        while(n!=0){
            int v=n&1;
            sb.insert(0,v+"");
            n=n>>1;
        }
        if(sb.length()==0){
            sb.append("0");
        }
        return sb.toString();
    }
    public static long fot1(int[] arr,int st,int en){
        if(st>en){
            return 0;
        }
        int[] a=new int[arr.length/2];
        int k=0;
        for(int i=st;i<en;i+=2){
            a[k]=arr[i]-arr[i+1];
            k++;
        }
        // for(int i=0;i<a.length;i++){
        //     System.out.print(a[i]+" ");
        // }
        // System.out.println();
        long val=maxSubArraySum(a);
        return val;
    }
    static long maxSubArraySum(int a[]) 
    { 
        int size = a.length; 
        long max_so_far = Long.MIN_VALUE, max_ending_here = 0; 
        int max=Integer.MIN_VALUE;
        for (int i = 0; i < size; i++) 
        { 
            max_ending_here = max_ending_here + a[i]; 
            if (max_so_far < max_ending_here) 
                max_so_far = max_ending_here; 
            if (max_ending_here < 0) 
                max_ending_here = 0; 
            max=Math.max(max,a[i]);
        } 
        if(max<0){
            return (long)0;
        }
        return max_so_far; 
    } 
    public static ArrayList<Integer> div(int n){
        ArrayList<Integer> r=new ArrayList<>();
        for(int i=2;i<=(int)Math.sqrt(n);i++){
            if(n%i==0){
                if(i%2==0&&(n/i)%2!=0)
                    r.add(i);
                else if(i%2!=0){
                    r.add(n/i);
                }
            }
        }
        return r;
    }
    public static String com(String s1,String s2){
        if(s1.compareTo(s2)>0){
            return s1;
        }else{
            return s2;
        }
    }
    public static void fost(HashMap<Integer,ArrayList<Integer>> map,int n,int n1){
        ArrayList<Integer> s=map.get(n);
        // System.out.println(s);
        for(int i=0;i<s.size();i++){
            if(s.get(i)==n1){
                s.remove(i);
                break;
            }
        }
        // System.out.println(s);
        for(int i=0;i<s.size();i++){
            fost(map,s.get(i),n);
        }
    }
    public static long calc(long n,long b){
        if(b==1){
            return Long.MAX_VALUE;
        }
        long val=(long)Math.ceil(n/b);
        long p=2;
        long ans=val;
        while(val!=0){
            val=(long)Math.ceil((long)n/(long)Math.pow(b,p));
            p++;
            ans+=val;
        }
        return ans;
    }
    public static long quala(long b){
        long val=(long)Math.ceil(Math.pow(1+(8*b),0.5));
        if(val%2==0){
            long va=(long)(2+val)/(long)2;
            return va;
        }else{
            long va=(long)(1+val)/(long)2;
            return va;
        }
    }
    public static String find(int a,int b){
        if(a==3){
            if(b==1){
                return "abb";
            }else if(b==2){
                return "bab";
            }else{
                return "bba";
            }
        }
        int v=((a-1)*(a-2))/2;
        if(b<=v){
            String s=find(a-1,b);
            StringBuffer sb=new StringBuffer(s);
            int diff=a-s.length();
            while(diff-->0){
                sb.insert(0,"a");
            }
            return sb.toString();
        }else{
            int diff=b-v;
            StringBuffer sb=new StringBuffer();
            sb.append("b");
            for(int i=0;i<a-diff-1;i++){
                sb.append("a");
            }
            sb.append("b");
            for(int i=0;i<diff-1;i++){
                sb.append("a");
            }
            return sb.toString();
        }
        // return new String("a"+s);
    }
    public static long z(long val){
        long p=1;
        while(val!=0){
            long rem=val%10;
            p*=rem;
            val=val/10;
        }
        return p;
    }
    public static int[] soe(int n){
        int[] arr=new int[n];
        arr[0]=0;
        for(int i=1;i<n;i++){
            if(arr[i]==0){
                for(int j=(2*(i+1))-1;j<n;j=j+i+1){
                    arr[j]=1;
                }
            }
        }
        return arr;
    }
    public static int[][] floydWarshall(int graph[][],int V){ 
        int dist[][] = new int[V][V]; 
        int i, j, k; 
        for (i = 0; i < V; i++){ 
            for (j = 0; j < V; j++){
                dist[i][j] = graph[i][j]; 
            }
        }
        for (k = 0; k < V; k++) { 
            for (i = 0; i < V; i++){ 
                for (j = 0; j < V; j++){ 
                    if (dist[i][k] + dist[k][j] < dist[i][j]){ 
                        dist[i][j] = dist[i][k] + dist[k][j]; 
                    }
                } 
            } 
        }
        return dist;
    } 
    public static class Comp implements Comparator<pair>{
        public int compare(pair a,pair b){
            if(a.x!=b.x){
                return b.x-a.x;
            }else{
                return a.y-b.y;
            }
        }
    }
    public static long gcd(long a,long b){
        if (b == 0) 
        return a; 
        return gcd(b, a % b);
    }
    public static int lcm(int a,int b){
        int x=Math.max(a,b);
        int y=Math.min(a,b);
        long ans=x;
        while(ans%y!=0){
            ans+=x;
        }
        if(ans>Integer.MAX_VALUE){
            return -1;
        }
        return (int)ans;
    }
    public static long fact(int n){
        long ans=1;
        for(int i=1;i<=n;i++){
            ans*=i;
        }
        return ans;
    }
}