#include<iostream>
#include<string.h>
using namespace std;

int StrtoInt(char *s){
	int num = 0;
	while(*s!='\0'){
		if( *s >= '0' && *s <= '9' ){
			num = num*10 + *s - '0';
			++s;
		}
	}
	return num;
}
void UpBabbleSort(char *r, const char *s){
	strcpy(r,s);

	for(int j=0;j<8;++j){
		for(int i=0;i<8-j;++i){
			if( r[i] <= r[i+1] ){
				char t = r[i];
				r[i] = r[i+1];
				r[i+1] = t;
			}
		}
	}
}

void DownBabbleSort(char *r, const char *s){
	UpBabbleSort(r,s);
	char temp[8+1];

	for(int i=0;i<8;++i)
		temp[i] = r[7-i];

	temp[8] = '\0';
	r = temp;
}



int main(){
	int n;
	char s[8+1];
	int max, min;
	char smax[9];
	char smin[9];

	cin >> n;
	for(int i=0;i<n;++i){
		cin >> s;


		UpBabbleSort(smax,s);
		max = StrtoInt(smax);

		DownBabbleSort(smin,s);
		min = StrtoInt(smin);

		cout << max-min << endl;

	}



}