#include <iostream>
#include "math.h"
using namespace std;
#define N 32
#define MAXINPUT 100
void sort(int input[]);
void calcmin(int input[],int *output);
void calc(int input[N][MAXINPUT],int n)
{
int i=0;
int output=0;
for(i=0;i<n;i++)
{
sort(input[i]);
calcmin(input[i],&output);
cout<<output<<endl;
}
}
int main()
{
int input[N][MAXINPUT];
int n;//输入的组数
cin>>n;
for(int i =0;i<n;i++)
{
cin>>input[i][0];
for(int j=1;j<=input[i][0];j++)
{
cin>>input[i][j];
}
}
calc(input,n);
getchar();
return 0;
}
void sort(int input[])
{
int index1=0;
int index2=0;
int temp=0;
for(index1=1;index1<=input[0];index1++)
{
for(index2=index1+1; index2<=input[0]; index2++)
{
if(input[index1]>input[index2])
{
temp= input[index1];
input[index1]=input[index2];
input[index2]=temp;
}
}
}
}
void calcmin(int input[],int *output)
{
int rslt = 0;
int temp1=0;
int temp2=0;
int max=0;
int divi = 0;
int remain = 0;
int i =0;
int kk=0;
rslt = int(input[0]/2);
max= input[0];
if(max%2)
{
//奇数
for(i=1;i<=rslt;i++)
{
temp1 = temp1+input[max-i+1]*pow(10,rslt-i);
temp2 = temp2+input[i]*pow(10,rslt-i+1);
}
temp2= temp2+input[rslt+1];
if(input[1]==0)
{
kk=pow(10,rslt-1);
remain=temp2%kk;
divi = temp2/(pow(10,rslt-1));
temp2= divi*pow(10,rslt)+remain;
}
}
else
{
//偶数
temp1 = input[rslt]*pow(10,rslt-1);
temp2 = input[rslt+1]*pow(10,rslt-1);
for(i=1;i<rslt;i++)
{
temp1=temp1+input[max+1-i]*pow(10,rslt-1-i);
temp2=temp2+input[i]*pow(10,rslt-1-i);
}
}
*output=temp2-temp1;
}