#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>

using namespace std;

int main(){
	
	int n,diff;
	char cdiff[8];
	string keta,max_kazu,min_kazu;
	
	cin>>n;
	
	for(int i=0;i<n;i++){
		
		cin>>keta;
		
		min_kazu=keta;
		max_kazu=keta;
		
		//昇順ソート
		stable_sort(min_kazu.begin(),min_kazu.end());
		//降順ソート
		stable_sort(max_kazu.begin(),max_kazu.end(),greater<char>());
		
		for(int j=7;j>=0;j--){
			if((max_kazu[j]-min_kazu[j])<0){
				diff=(10+(max_kazu[j]-'0'))-(min_kazu[j]-'0');
				max_kazu[j-1]--;
			}
			else{
				diff=(max_kazu[j]-'0')-(min_kazu[j]-'0');
			}
			cdiff[j]=diff+'0';
		}
		
		cout<<cdiff<<endl;
		
		//string 型の初期化
		keta=""; 
		max_kazu="";
		min_kazu="";
		memset(cdiff,'\0',sizeof(cdiff));
	}
}