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

void main() {
    int num;

    num = readln.chomp.to!int;
    foreach (i; 0..num) {
        char[] input;
        int[] data;
        int max, min;

        input = cast(char[])(readln.chomp);
        foreach (temp; input) data ~= (temp - '0').to!int;

        data.sort;
        min = data.map!(to!string).join.to!int;
        data.reverse;
        max = data.map!(to!string).join.to!int;

        writeln(max-min);
    }
}