let to_list s =
  let rec loop l = function
      -1 -> l
    | i -> loop (((Char.code s.[i]) - 48)::l) (i-1)
  in loop [] ((String.length s)-1)

let () =
  let n = read_int () in
  let rec read = function
      0 -> ()
    | i ->
       let l = read_line () |> to_list |> List.sort compare in
       let small = List.fold_left (fun x y -> 10*x + y) 0 l and
           large = List.fold_right (fun x y -> x + 10*y) l 0 in
       Printf.printf "%d\n" (large-small);
       read (i-1)
  in read n
;;