#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

int toInt(string input){
	int times = 1;
	int res = 0;
	for(int i = input.size() - 1; i >= 0; i--){
		res += (input[i] - '0') * times;
		times *= 10;
	}
	return res;
}

int main(){
	int n = 0;
	while(cin >> n){
		if(cin.eof()) break;
		vector<string> input(n);
		for(int i = 0; i < n; i++){
			string tmp;
			cin >> tmp;
			input[i] = tmp;
		}

		for(int i = 0; i < n; i++){
			sort(input[i].begin(), input[i].end());
			int min = toInt(input[i]);

			sort(input[i].begin(), input[i].end(), greater<int>());
			int max = toInt(input[i]);

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