#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;
string sub (string a, string b)
{
//	reverse (a.begin(), a.end() );
//	reverse (b.begin(), b.end() );
/*
	while (a.length() != b.length()){
		if (a.length() > b.length()){
			b += '0';
		}else if (a.length() < b.length() ){
			a += '0';
		} // end if
	} // end while
*/
	int i;
	string res = "";
	bool blow = false;

	for (i = a.length() - 1; i >= 0; --i){
		int an = a[i] - '0';
		int bn = b[i] - '0';
		if (blow ){
			--an;
			blow = false;
		} // end if

		if (an < bn){
			blow = true;
			an += 10;
		}else{
			blow = false;
		} // end if

		res += (an - bn) + '0';
	} // end for

	reverse (res.begin(), res.end() );

	return res;
}
int main()
{
//	cut here before submit 
//	freopen("testcase.diff2", "r", stdin);
	string str = "";
	int n;

	getline (cin, str );
	stringstream ssn(str);
	ssn >> n;

	int i;
	for (i = 0; i < n; ++i){
		getline (cin, str );
		string res = "";
		string org = str;
		sort (str.begin(), str.end() );
		string min = str;
		sort (str.begin(), str.end(), greater<int>() );
		string max = str;
		cout << "num: " << org << endl;
		cout << "max: " << max << endl;
		cout << "min: " << min << endl;
		res = sub (max, min );
		while (res[0] == '0' && res.length() > 1){
			res.erase(res.find('0'), 1);
		} // end while

		cout << res << endl;	

	} // end loop

		
	return 0;
}