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

long to_digit(long[] numbers) {return numbers.reduce!("a * 10 + b");}

void main() {
  int times = readln.chomp.to!(int);
  foreach(t; 0..times) {
    long[] numbers = readln.chomp.dup.map!((x) => to!(long)(x - '0')).array.sort.array;
    writeln(numbers.reverse.to_digit - numbers.reverse.to_digit);
  }
}