import std.stdio,std.string,std.conv,std.algorithm;

int main()
{
	int n = readln.chomp.to!int;
	while(n--)
	{
		string s = readln.chomp;
		int[] a;
		a.length = s.length;
		foreach(i;0..s.length)
		{
			a[i] = cast(int)(s[i] - '0');
		}

		sort(a);
		int count = 1;
		int num_max = 0,num_min = 0;
		foreach(i;0..a.length)
		{
			num_max += a[i]*count;
			num_min += a[a.length - i -1]*count;
			count *= 10;
		}

		writeln(num_max - num_min);
	}
	return 0;
}