#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <cstdlib>

using namespace std;

int main()
{
	char n1[9];
	int n, big, min;
	int i;
	
	cin >> n;
	
	for(i = 0; i < n; i++){
		cin >> n1;
		
		sort(n1, n1+7);
		min = atoi(n1);
		sort(n1, n1+7, greater<int>());
		big = atoi(n1);
		
		cout << big - min << endl;
	}
	
	return 0;
}