#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int c1(const void *a, const void *b){return *(char*)a-*(char*)b;}
int c2(const void *a, const void *b){return *(char*)b-*(char*)a;}
signed main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char str[10];
		scanf("%s", str);
		qsort(str,8,sizeof(char), c1);
		int x1 = atoi(str);
		qsort(str,8,sizeof(char), c2);
		int x2 = atoi(str);
		cout << x2-x1 << endl;
	}
	return 0;
}