#include<iostream>
#include<string>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	string str;
	
	cin >> n;
	while (n-- > 0) {
		cin >> str;

		sort(str.begin(), str.end());
		
		int min = 0, max = 0;
		for (int i = 0; i < 8; i++) {
			min = min * 10 + str[i];
			max = max * 10 + str[7 - i];
		}

		cout << max - min << endl;
	}
	return 0;
}
