#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <sstream>

using namespace std;

int DESC(char a,char b){
	if(a > b){
		return -1;
	}else{
		return 1;
	}
}
int ASC(char a,char b){
	if(a > b){
		return 1;
	}else{
		return -1;
	}
}

void selectSort(string& str,int (*cmp)(char,char)){
	for(int i = 0;i < str.size();i++){
		int index = i;
		for(int j = i + 1;j < str.size();j++){
			if(cmp(str[index],str[j]) == 1){
				index = j;
			}
		}
		swap(str[i],str[index]);
	}
}

int main(void){
	int n;
	string str;
	cin >> n;
	cin.get();
	for(int i = 0;i < n;i++){
		getline(cin,str);
		string asc, dsc;
		asc = str;
		dsc = str;
		selectSort(asc,ASC);
		selectSort(dsc,DESC);
		long long d, a;
		stringstream ss1(dsc);
		ss1 >> d;
		stringstream ss2(asc);
		ss2 >> a;
		cout << (d - a) << endl;
	//	cout << (atoi(dsc) - atoi(asc)) << endl;
	}
	return 0;
}