import Data.List (sort)
import Control.Applicative ((<$>))
import Control.Monad (replicateM_)

main = do
  n <- readLn
  replicateM_ n $ putStrLn =<< show.(\x -> maxNum x - minNum x)<$>getLine
maxNum :: String -> Int
maxNum = read.reverse.sort

minNum :: String -> Int
minNum = read.sort