#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string str,x,y;
int n,a,b;
int main(){
	cin>>n;
	while(n--){
		cin>>str;
		x=str;
		sort(x.begin(),x.end());
		y=x;
		reverse(y.begin(),y.end());
		a=b=0;
		for(int i=0;i<(int)str.size();i++){
			a+=x[i];b+=y[i];
			a*=10;b*=10;
		}
		cout<<(b-a)/10<<endl;
	}
	return 0;
}