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

public class Main {
    static PrintWriter pw;
    static Scanner sc;
    static int inf=(int)1e9;
    static int[][] arr;
    static double[][] dp;
    static Pair[] pos;
    static int[] skip;
    static long ceildiv(long x, long y){ return (x+y-1)/y;}
    public static void main(String[] args) throws Exception {
        sc = new Scanner(System.in);
        pw = new PrintWriter(System.out);
        int t=sc.nextInt();
        all:while(t-->0){
            String s=sc.nextLine();
            int max=2;
            for(char a='0';a<='8'; a++){
                for(char b=(char)(a+1); b<='9'; b++)
                    max=Math.max(test(a, b, s), max);
            }
            max-=max%2;
            int[] freq= new int[10];
            for(int i=0; i<s.length(); i++)
                freq[s.charAt(i)-'0']++;
            for(int x: freq)
                max=Math.max(x, max);
            pw.println(s.length()-max);
        }
        pw.close();
    }
    static int test(char a, char b, String s){
        char prev=' ';
        int x=0;
        for(int i=0; i<s.length(); i++){
            if((s.charAt(i)==a || s.charAt(i)==b) && s.charAt(i)!=prev){
                prev=s.charAt(i);
                x++;
            }
        }
        return x;
    }
    static void printArr(int[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(long[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(Integer[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(char[] arr){
        for(int i=0; i<arr.length; i++)
            pw.print(arr[i]);
        pw.println();
    }
    static void printArr(ArrayList list){
        for(int i=0; i<list.size(); i++)
            pw.print(list.get(i)+" ");
        pw.println();
    }
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;
        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public int[] nextArr(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < arr.length; i++)
                arr[i] = nextInt();
            return arr;
        }
        public Integer[] nextsort(int n) throws IOException{
            Integer[] arr=new Integer[n];
            for(int i=0; i<n; i++)
                arr[i]=nextInt();
            return arr;
        }
        public Pair nextPair() throws IOException{
            return new Pair(nextInt(), nextInt());
        }
        public Pair[] nextPairArr(int n) throws IOException{
            Pair[] arr=new Pair[n];
            for(int i=0; i<n; i++)
                arr[i]=new Pair(nextInt(), nextInt());
            return arr;
        }
        public boolean ready() throws IOException {
            return br.ready();
        }
    }
    static class Pair implements Comparable<Pair>{
        int x;
        int y;
        public Pair(int x, int y) {
            this.x=x;
            this.y=y;
        }
        public int hashCode() {
            return this.x*1000+this.y;
        }
        public int compareTo(Pair p) {
            return this.y-this.x-p.y+p.x;
        }
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this.getClass() != obj.getClass()) {
                return false;
            }
            Pair p = (Pair) obj;
            return this.x==p.x && this.y==p.y;
        }
        public boolean equal(Pair p){
            if(this.x==p.x)
                return this.y==p.y;
            if(this.x==p.y)
                return this.y==p.x;
            else
                return false;
        }
        public Pair clone(){
            return new Pair(x, y);
        }
        public String toString(){
            return this.x+" "+this.y;
        }
    }
    static class Triple implements Comparable<Triple>{
        int x;
        int y;
        char c;
        public Triple(int x, int y, char c){
            this.x=x;
            this.y=y;
            this.c=c;
        }
        public int compareTo(Triple t){
            return this.y-t.y;
        }
        public String toString(){
            if(this.x==1)
                return "INSERT "+(this.y+1)+" "+this.c;
            else if(this.x==0)
                return "REPLACE "+(this.y+1)+" "+this.c;
            else
                return "DELETE "+(this.y+1);
        }
    }
    static class decimal{
        int x;
        int dec;
        public decimal(String s){
            x=Integer.parseInt(s.substring(0, s.length()-3));
            dec=Integer.parseInt(s.substring(s.length()-2));
        }
        public decimal(int x, int dec){
            this.x=x;
            this.dec=dec;
        }
        public decimal plus(decimal a){
            return new decimal(this.x+a.x+(this.dec+a.dec)/100, (this.dec+a.dec)%100);
        }
        public decimal minus(decimal a){
            return new decimal(this.x-a.x-(this.dec>=a.dec?0:1), (this.dec+100-a.dec)%100);
        }
        public decimal min(decimal a){
            if(a.x>this.x)
                return this;
            if(this.x>a.x)
                return a;
            if(a.dec>this.dec)
                return this;
            return a;
        }
        public String toString(){
            return this.x+"."+this.dec;
        }
        public decimal clone(){
            return new decimal(this.x, this.dec);
        }
        public boolean equal(decimal a){
            return a.x==this.x && a.dec==this.dec;
        }
    }
}
