/*
	0051:Differential II
*/
#include	<iostream>
#include	<cstdio>
#include	<cstdlib>
#include	<algorithm>
#include	<functional>

using namespace std;

int main(void) {
	int n;
	
	cin >> n;
	
	while(n-- > 0) {
		int a, b, dif;
		char str[9];
		scanf("%s", str);
		sort(str, str+8);
		a = atoi(str);
		sort(str, str+8, greater<char>() );
		b = atoi(str);
		
		dif = b - a;
		cout << dif << endl;
	}
	
	return 0;
}