#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string S;
int result(void){
	sort(S.begin(),S.end());
	int min=atoi(S.c_str());
	reverse(S.begin(),S.end());
	int max=atoi(S.c_str());
	return max-min;

}

int main(void){
	int n;
	cin >>n;
	for(int i=0;i<n;i++){
		cin >> S;
		cout << result() << endl; 
	}
	return 0;
}