var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr=(input.trim()).split("\n");
Arr.shift();
Arr.forEach(function(v){
var arr=v.split("").map(Number);
arr.sort(function(a,b){return b-a;});
var a=arr.join("");
arr.sort(function(a,b){return a-b;});
var b=arr.join("");
console.log(parseInt(a)-parseInt(b));
});