#include <stdio.h>
#include <math.h>
#include <iostream>

using namespace std;

int main() {

	int s[8];
	int d;
	char g[8];
	int kari;
	int kazumax = 0;
	int kazumin = 0;

	cin >> d;

	for (int i = 0; i < d; i++) {
		cin >> g[0] >> g[1] >> g[2] >> g[3] >> g[4] >> g[5] >> g[6] >> g[7];

		for (int i = 1; i <= 8; i++) {
			s[i] = g[i] - '0';
		}
		for (int i = 8; i > 0 ; i--) {
			for (int j = 0; j < i; i++) {
				if (s[j] > s[j+1]) {
					kari = s[j];
					s[j] = s[j+1];
					s[j+1] = kari;
				}
			}
		}

		for (int i = 0; i < 8; i++){
			kazumax += s[i] * pow(10.0,(double)i);
		}
		for (int i = 7; i >= 0; i--) {
			kazumin += s[i] * pow(10.0,(double)i);
		}

		cout << kazumax - kazumin << endl;
	}



	return 0;
}