#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	vector<long long>l;
	char str[8];
	int k[8];
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> str;
		for (int i = 0; i < 8; i++)
			k[i] = str[i] - '0';
		sort(k, k + 8);
		l.push_back((k[7] - k[0]) * 9999999 + (k[6] - k[1]) * 999990 + (k[5] - k[2]) * 99900 + (k[4] - k[3] * 9000));
	}
	for (long long i:l)
		cout << i << endl;
}