function main() {
  var
  i,
  l, h,
  line,
  n = parseInt(input[0], 10),
  lines = input.slice(1);
  for(i = 0; i < n; i++) {
    line = lines[i].split('').map(parseFloat);
    line.sort(function(a, b){return a - b;});
    min = parseInt(line.join(''), 10);
    line.sort(function(a, b){return b - a;});
    max = parseInt(line.join(''), 10);
    console.log(max - min);
  }
}

var
input = '';

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
process.stdin.on('end', function() {
  input = input.split('\n');
  main();
});