answers = Array.new
n = $stdin.gets.chomp.to_i
1.upto( n ){
	digit = []
	s = $stdin.gets.chomp
	list = s.split(//).map{|n| n.to_i}
	min = list.sort{ |a,b| a <=> b }.inject{ |memo,item| memo * 10 + item}
	max = list.sort{ |a,b| b <=> a }.inject{ |memo,item| memo * 10 + item}
	answers << (max - min)
}
answers.each{ |a| puts a }
