#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
	int n;
	string s;
	cin >> n;
	vector<int> a;
	for (int i = 0; i < n; i++) {
		cin >> s;
		sort(s.begin(), s.end());
		long long int x = stol(s);
		reverse(s.begin(), s.end());
		long long int y = stol(s);
		cout << y - x << endl;

	}
	return 0;
}