#include<iostream>
#include<cstdio>
#include<algorithm>
#include<deque>

using namespace std;

int main()
{
	int max,min;
	deque<char> nums;
	int n;
	int k;
	char c;
	cin>>n;
	while(n){
		k=10000000;
		max=min=0;
		n--;
		for(int i=0;i<8;i++){
			cin>>c;
			nums.push_back(c);
		}

		sort(nums.begin(),nums.end());
		deque<char>::iterator it= nums.begin();
		while(it!= nums.end()){
			min+=((int)*it-(int)'0')*k;
			k/=10;	
			++it;
		
		}
		k=10000000;
		reverse( nums.begin(), nums.end() );  
		it= nums.begin();
		while(it!= nums.end()){
			max+=((int)*it-(int)'0')*k;
			k/=10;
			++it;
		}
		cout<<max-min<<endl;
		nums.clear();
	}

	return 0;
}