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

using namespace std;

int main() {

	int s[8];
	int d;
	char g[8];
	int kari;
	int l1,l2,l3,l4,l5,l6;


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

		l1 = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
		l2 = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];
		l3 = s[7] * 1000 + s[6] * 100 + s[5] * 10 + s[4];
		l4 = s[3] * 1000 + s[2] * 100 + s[1] * 10 + s[0];

		l5 = l3 - l1;
		l6 = l4 - l2;

		if (l6 < 0) {
			l5--;
			l6 += 10000;
		}
		cout << l5 << l6 << endl;
	}



	return 0;
}