object Main extends App {
  val src = io.Source.stdin.getLines.map(_.toInt)
  src.next

  def myDiff(org: Int): Int = {
    val min = org.toString.sorted
    val max = min.reverse
    max.toInt - min.toInt
  }

  for (num <- src) println(myDiff(num))
}