#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int f(string s){
	stringstream ss;
	int n;
	ss<<s;
	ss>>n;
	return n;
}
int main(void){
	string a;
	unsigned int n,x;
	cin>>n;
	while(n--){
		cin>>a;
		sort(a.begin(),a.end());
		x=f(a);
		reverse(a.begin(),a.end());
		cout<<f(a)-x<<"\n";
	}
} 