#include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("lld", &x); return (x); }

int main()
{
	int n;
	char num[9];
	n = in();

	loop(i, 0, n)
	{
		scanf("%s", num);
		int maxNum = 0;
		int minNum = 0;
		sort(num, num + 8);
		loop(j, 0, 8)
		{
			minNum = minNum * 10 + (num[j] - '0');
		}
		sort(num, num + 8, greater<char>());
		loop(j, 0, 8)
		{
			maxNum = maxNum * 10 + (num[j] - '0');
		}
		printf("%d\n", maxNum - minNum);
	}
	return (0);
}