#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <utility>
#include <numeric>

using namespace std;
typedef long long llong;
typedef unsigned long long ullong;

const ullong MOD = 1000000000 + 7;

int main()
{
	int n;
	for (cin >> n; n--;)
	{
		string a, b;

		cin >> a;

		sort(a.begin(), a.end());

		b = a;
		reverse(b.begin(), b.end());

		cout << atoi(b.c_str()) - atoi(a.c_str()) << endl;

	}
}
