let id x = x

let lst_to_num lst =
  List.fold_left (fun s v -> s * 10 + v) 0 lst

let differential s =
  let lst = List.map int_of_string (Str.split (Str.regexp "") s) in
  let lstmn = List.sort compare lst in
  let lstmx = List.sort (fun x y -> compare y x) lst in
  (lst_to_num lstmx) - (lst_to_num lstmn)

let _ = 
  let n = Scanf.scanf "%d\n" id in
  for i = 1 to n do
    let x = Scanf.scanf "%s\n" differential in
    Printf.printf "%d\n" x
  done