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

int main()
{
    int n , sa;
    string s , t;
    cin >> n;
    while(n--){
        cin >> s:
        t=s;
        sort(s.begin() , s.end());
        reverse(t.begin() , t.end());
        sa=atoi(s.c_str())-atoi(t.c_str);
        cout << sa << endl;
        }
    }
	return 0;
}