#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <functional>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

std::vector<int> str2vec(std::string str){
	std::vector<int> v;

	v.resize(str.size(), 0);
	for(int i = 0; i < str.size(); i++){
		v[i] = str[i] - '0';
	}

	return v;
}

long vec2l(std::vector<int> v){
	long result = 0;
	long d = 1;

	for(int i = 0; i < v.size(); i++){
		result += v[i] * d;
		d *= 10;
	}

	return result;
}

int main(){
	int n;
	std::string buf;
	std::vector<int> v;
	long min, max;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
#endif

#if INPUT_FROM_FILE
	std::getline(ifs, buf);
#else
	std::getline(std::cin, buf);
#endif
	n = std::stoi(buf);
	
	for(int i = 0; i < n; i++){
#if INPUT_FROM_FILE
		std::getline(ifs, buf);
#else
		std::getline(std::cin, buf);
#endif
		v = str2vec(buf);

		std::sort(v.begin(), v.end());
		max = vec2l(v);

		std::sort(v.begin(), v.end(), std::greater<int>());
		min = vec2l(v);

		std::cout << max - min << std::endl;
	}

	return 0;
}
