#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
  int i ,n ,large ,small ,ans;
  string nam ,tmp;
  cin >> n;
  for( i=0 ; i<n ; i++ ){
    cin >> nam;
    sort(nam.begin(),nam.end());
    small = atoi(nam.c_str());

    sort( nam.rbegin() ,nam.rend());
    large = atoi(nam.c_str());

    ans = large - small;

    cout << ans << endl;  
  }

  return 0;
}