#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    long int max,min;
    char s1[8];
    int n;
    cin >> n;
    while(n--){
        min = 0;
        max = 0;
        cin >> s1;
        sort(s1,s1 + 8);
        for(int i=7,ten=1;i>=0;i--,ten*=10) min += (s1[i]%'0')*ten;
        for(int i=0,ten=1;i<8;i++,ten*=10) max += (s1[i]%'0')*ten;
        cout << max - min << endl;
    }
}