import java.util.Scanner;
public class Differ {
public static int[] data=new int[10];
public static int data_length;
public static int[] quanPai;
public static int[] quanPaiflag;
public static int minResult;
public static int[] ResultArr;
public static int cut_posi;
public static int absoluteValue(){
int numA=0;
int numB=0;
for(int i=0;i<cut_posi;i++){
numA=numA*10+data[quanPai[i]];
}
for(int i=cut_posi;i<data_length;i++){
numB=numB*10+data[quanPai[i]];
}
int tempabsoluteValue=numA-numB;
return tempabsoluteValue>0?tempabsoluteValue:-tempabsoluteValue;
}
public static void dFS(int Layer){
if(Layer==data_length){
/*count++;
for(int i=0;i<data_length;i++){
System.out.print(quanPai[i]);
}
System.out.println();
*/
if(data_length>=4){
if(data[quanPai[0]]!=0&&data[quanPai[cut_posi]]!=0){
int tempResult=absoluteValue();
if(minResult>tempResult){
minResult=tempResult;
}
}
}
if(data_length==3){
if(data[quanPai[cut_posi]]!=0){
int tempResult=absoluteValue();
if(minResult>tempResult){
minResult=tempResult;
}
}
}
if(data_length==2){
int tempResult=absoluteValue();
if(minResult>tempResult){
minResult=tempResult;
}
}
return;
}
for(int i=0;i<data_length;i++){
if(quanPaiflag[i]==0){
quanPai[Layer]=i;
quanPaiflag[i]=1;
dFS(Layer+1);
quanPaiflag[i]=0;
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String test=sc.nextLine();
int testCase=test.charAt(0)-'0';
ResultArr=new int[testCase];
int ResultArr_pc=0;
for(int i=0;i<testCase;i++){
minResult=0x7fffffff;
int data_count=0;
String str=sc.nextLine();
int stri=0;
while(stri<str.length()){
char tempChar=str.charAt(stri++);
if(tempChar!=' ') data[data_count++]=tempChar-'0';
}
data_length=data_count;
if(data_length==10){
minResult=247;
}else{
cut_posi=data_length/2;
quanPai=new int[data_length];
quanPaiflag=new int[data_length];
dFS(0);
}
ResultArr[ResultArr_pc++]=minResult;
}
//System.out.println(count);
//System.out.println();
for(int i=0;i<testCase;i++){
System.out.println(ResultArr[i]);
}
}
}