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

int main(){
	int n;
	cin >> n;

	for(int i = 0; i < n; i++){
		int s;
		cin >> s;
		int max = 0, min = 0;
		vector<int> vec;
		int p = 10000000;
		for(int j = 0; j < 8; j++){
			vec.push_back(s / p);
			s = s % p;
			p /= 10;
		}
		sort(vec.begin(), vec.end());
		int maxP = 1;
		int minp = 10000000;
		for(int j = 0; j < 8; j++){
			min += vec[j] * minp;
			minp /= 10;
			max += vec[j] * maxP;
			maxP *= 10;
		}
		cout << max - min << endl;

	}
	return 0;
}