if __FILE__ == $0
  n = gets.to_i
  n.times do 
    nums = gets.rstrip.split('').map{|i| i.to_i}
    max_num = nums.sort.reverse.join.to_i
    min_num = nums.sort.join.to_i
    puts max_num - min_num
  end
end