import Control.Monad
import Data.Char
import Data.List

digitsToNum :: [Int] -> Int
digitsToNum dig = foldl (\x y->x*10+y) 0 dig

solve :: [Int] -> IO ()
solve dig = do
    let nmin = digitsToNum $ sort dig
    let nmax = digitsToNum $ reverse $ sort dig
    print(nmax-nmin)

main :: IO ()
main = do
    n_dat<-readLn::IO Int
    str<-replicateM n_dat getLine
    mapM_ solve $ map (\x->map (\y->digitToInt(y)) x) str