#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
#include <stdlib.h>
using namespace std;
int n,num1[8],m,M;

int henkan()
{
  int num = 0;
  for(int i = 0;i < 8 ;i++){
    num+=num1[i]*pow(10,7-i);
  }
  return num;
}


int main(){

  char num;
  cin >> n;
  for(int i = 0; i < n ; i++ ){
    m = M = 0;
    for(int j = 0;j < 8 ;j++ )
      {
	cin >> num;
	num1[j] =  num-'0';
      }
    sort(num1,num1+8);
    m =  henkan();

    sort(num1,num1+8,greater<int>());
    M = henkan();

    cout << M - m << endl;
  }
  return 0;
}