#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
	int n;
	string s;
	int max_x, min_x;
	cin >> n;

	rep(i, n){
		cin >> s;
		sort(s.begin(), s.end());
		min_x = stoi(s);
		sort(s.begin(), s.end(), greater<char>());
		max_x = stoi(s);

		cout << max_x - min_x << endl;
	}
}