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

int2list :: (Read a, Show a, Integral a) => a -> [a]
int2list n = (map (read . (: "")) . show) n

list2int :: (Read a, Show a, Integral a) => [a] -> a
list2int ns = (read . concat . map show) ns

diff :: (Read a, Show a, Integral a) => a -> a
diff n = maxn - minn
    where
        nlist = sort $ int2list n
        minn = list2int nlist
        maxn = list2int $ reverse nlist

main :: IO ()
main = do
    n <- read <$> getLine
    ds <- replicateM n $ show . diff . read <$> getLine
    putStr $ unlines ds