#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <cstring>

using namespace std;

int main(void)
{
	int n;

	cin >> n;

	for( int i = 0;i < n;i++ ){
		string input;
		cin >> input;

		sort( input.begin(), input.end() );

		unsigned int min = atoi( input.c_str() );

		sort( input.begin(), input.end(), greater<char>() );

		unsigned int max = atoi( input.c_str() );

		cout << max - min << endl;
	}

	return 0;
}