import Control.Monad
import Data.Char
import Control.Applicative

decomp :: String -> [Int]
decomp = map digitToInt

qsort :: [Int] -> [Int]
qsort [] = []
qsort (p:xs) = qsort lt ++ [p] ++ qsort gteq
  where lt   = [x | x <- xs, x <  p]
        gteq = [x | x <- xs, x >= p]

converter :: [Int] -> Int
converter xs = read (concatMap show xs)


diff :: [Int] -> Int
diff xs =
  let qs = qsort xs
  in
      converter (reverse qs) - converter qs

solver :: [String] -> [Int]
solver xs =
  let ys = map decomp xs
  in
      map diff ys


main :: IO()
main = do
  n <- (read :: String -> Int) <$> getLine
  contents <- replicateM n getLine
  mapM_ print $ solver contents