#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>

using namespace std;

int main()
 {
	 int n, big, min;
	 char c[9];

	 cin >> n;

	 for(int i = 0; i < n; i++){
		 cin >> c;

		 sort (c, c+8);

		 min = atoi(c);

		 sort (c, c+8, greater<int>());

		 big = atoi(c);

		 cout << big - min << endl;
	 }

	 return 0;
}