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

int main(){
	int n;
	cin>>n;
	int data[8];
	int max;
	int min;
	int ans[n];
	for(int i=0;i<n;i++){
		int a;
		cin >> a;
		data[0] = a%10;
		data[1] = a%100/10;
		data[2] = a%1000/100;
		data[3] = a%10000/1000;
		data[4] = a%100000/10000;
		data[5] = a%1000000/100000;
		data[6] = a%10000000/1000000;
		data[7] = a%100000000/10000000;
	sort(data,data+8);
	max = data[7]*10000000+data[6]*1000000+data[5]*100000+data[4]*10000+
		  data[3]*1000+data[2]*100+data[1]*10+data[0];
	min = data[0]*10000000+data[1]*1000000+data[2]*100000+data[3]*10000+
		  data[4]*1000+data[5]*100+data[6]*10+data[7];
	ans[i] = max-min;
	
	}
	for(int i=0;i<n;i++){
		cout << ans[i]<<endl;
	}
	return 0;
}
		