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

int main()
{
    int n;
    string s;

    cin >> n;

    while( n-- )
    {
        cin >> s;

        string min, max = "";
        for( int i = 0; i < 7; i++ )
        {
            for( int j = i + 1; j < 8; j++ )
            {
                if( s[i] - '0' > s[j] - '0' )
                {
                    char temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
        min = s;

        for( int i = 7; i >= 0; i-- )
            max += min[i];
        
        int mi = 0, ma = 0, mul = 1;
        for( int i = 7; i >= 0; i-- )
        {
            mi += ( min[i] - '0' ) * mul;
            ma += ( max[i] - '0' ) * mul;
            mul *= 10;
        }
        cout << ma - mi << endl;
    }
    return 0;
}
