When you enter 8 numbers from 0 to 9, write a program that outputs the difference between the largest integer and the smallest integer that can sort the 8 numbers. The number that can be sorted may start from 0, such as 00135569.



Input

Given multiple datasets. The number of datasets n (n ≤ 50) is given on the first line. Then n rows of data are given. Each data is a sequence of 8 numbers (0 to 9 numbers).

Output

For each dataset, output the difference between the largest and smallest integers that can be rearranged in the entered numbers on one line.

Example

Input

2
65539010
65539010


Output

96417531
96417531
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main{
	public static void main(String[] args){
		BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
		String str="";
		int[] hairetu=new int[8];
		String max="",min="";

		try {
			int d_set;
			str=bfr.readLine();
			d_set=Integer.parseInt(str);

			for(int i=0; i<d_set; i++) {
				str="";
				min="";
				max="";
				str=bfr.readLine();
				for(int j=0; j<8; j++) {
					hairetu[j]=Integer.parseInt(str.charAt(j)+"");
				}
				hairetu=mSort(hairetu);

				for(int k=0; k<8; k++) {
					min=min+""+hairetu[k];
					max=max+""+hairetu[7-k];
				}
				System.out.println(Integer.parseInt(max)-Integer.parseInt(min));
			}
		}catch(IOException e) {

		}
	}

	public static int[] mSort(int array[])  {
		if(array.length > 1) {
			int elementsInA1 = array.length/2;
			int elementsInA2 = array.length - elementsInA1;
			int arr1[] = new int[elementsInA1];
			int arr2[] = new int[elementsInA2];

			for(int i = 0; i < elementsInA1; i++)
				arr1[i] = array[i];

			for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++)
				arr2[i - elementsInA1] = array[i];

			arr1 = mSort(arr1);
			arr2 = mSort(arr2);

			int i = 0, j = 0, k = 0;

			while(arr1.length != j && arr2.length != k) {
				if(arr1[j] <= arr2[k]) {
					array[i] = arr1[j];
					i++;
					j++;
				} else {
					array[i] = arr2[k];
					i++;
					k++;
				}
			}

			while(arr1.length != j) {
				array[i] = arr1[j];
				i++;
				j++;
			}
			while(arr2.length != k) {
				array[i] = arr2[k];
				i++;
				k++;
			}
		}
		return array;
	}
}
