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

public class Dec24 {

	public static void main(String[] args) throws Exception {

//			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//			int t = Integer.parseInt(br.readLine());
//			StringBuilder sb = new StringBuilder();
//			while (t-- > 0) {
//				String[] scn = (br.readLine()).trim().split(" ");
//				int n = Integer.parseInt(scn[0]);
//				long[] arr = new long[n];
//				scn = (br.readLine()).trim().split(" ");
//				for (int i = 0; i < n; i++) {
//					arr[i] = Long.parseLong(scn[i]);
//				}
//				sb.append("\n");
//			}
//			System.out.println(sb);
//			return;

//		first();
//		sec();
//		third();
//		four();
		fif();
//				six();
	}

	private static void first() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		while (t-- > 0) {
			String[] scn = (br.readLine()).trim().split(" ");
			int n = Integer.parseInt(scn[0]);
			double ang = 180 / (double) (2 * n);
			double angInRad = Math.toRadians(ang);
			double ans = 1.0 / Math.tan(angInRad);
			sb.append(String.format("%.7f", ans));
			sb.append("\n");
		}
		System.out.println(sb);
		return;
	}

	private static void sec() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();

		String[] scn = (br.readLine()).trim().split(" ");
		int n = Integer.parseInt(scn[0]);
		long[] arr = new long[n];
		scn = (br.readLine()).trim().split(" ");
		long pos = 0, neg = 0;
		for (int i = 0; i < n; i++) {
			arr[i] = Long.parseLong(scn[i]);
			if (arr[i] < 0)
				neg += arr[i];
			else
				pos += arr[i];
		}

		System.out.println(pos - neg);
		return;
	}

	private static void third() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] scn = (br.readLine()).trim().split(" ");
		long n = Long.parseLong(scn[0]);
		long m = Long.parseLong(scn[1]);
		while (true) {
			if (n == 0 || m == 0)
				break;
			if (n >= 2l * m) {
				long x = (n - 2l * m) / (2l * m);
				n -= x * 2l * m;
				if (n >= 2l * m) {
					n -= 2l * m;
				}
			} else if (m >= 2l * n) {
				long x = (m - 2l * n) / (2l * n);
				m -= x * 2l * n;
				if (m >= 2l * n) {
					m -= 2l * n;
				}
			} else {
				break;
			}

		}
		sb.append(n + " " + m);
		System.out.println(sb);
		return;
	}

	private static void four() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str = br.readLine();
		int n = str.length();
		if (n < 26) {
			System.out.println("-1");
		} else {
			String pat = "abcdefghijklmnopqrstuvwxyz";
			StringBuilder sb = new StringBuilder();

			int j = 0;
			for (int i = 0; i < n; i++) {
				if (j < 26 && str.charAt(i) <= pat.charAt(j)) {
					sb.append(pat.charAt(j));
					j += 1;
				} else {
					sb.append(str.charAt(i));
				}

			}
			if (j == 26)
				System.out.println(sb);
			else
				System.out.println("-1");
		}
		return;
	}

	private static void fif() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();

		while (t-- > 0) {
			String str = br.readLine();
			int n = str.length();
			int max = 0;
			for (int dig = 0; dig < 10; dig++) {
				int[] arr = new int[10];
				HashSet<Integer> set = new HashSet<>();
				int count = 0;
				for (int i = 0; i < n; i++) {
					if (str.charAt(i)-'0' == dig) {
						for (int ele : set)
							arr[ele] += 1;
						count += 1;
						set = new HashSet<>();
					} else {
						set.add(str.charAt(i) - '0');
					}
				}
				int lmax = 0;
				for (int ele : arr) {
					lmax = Math.max(lmax, ele);
				}
				max = Math.max(max, Math.min(count, lmax));
			}
			max*=2;
			int[] arr = new int[10];
			for(char ch : str.toCharArray()) {
				arr[ch-'0']+=1;
			}
			for(int ele:arr) {
				max = Math.max(ele, max);
			}
			sb.append(n - max);
			sb.append("\n");
		}
		System.out.println(sb);
		return;
	}

	private static void six() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();

		while (t-- > 0) {
			String[] scn = (br.readLine()).trim().split(" ");
			int n = Integer.parseInt(scn[0]);
			long[] arr = new long[n];
			scn = (br.readLine()).trim().split(" ");
			for (int i = 0; i < n; i++) {
				arr[i] = Long.parseLong(scn[i]);
			}

			sb.append("\n");
		}
		System.out.println(sb);
		return;
	}

	public static void sort(long[] arr) {
		int n = arr.length;
		for (int i = 0; i < n; i++) {
			int idx = (int) Math.random() * n;
			long temp = arr[i];
			arr[i] = arr[idx];
			arr[idx] = temp;
		}
		Arrays.sort(arr);
	}
}
