#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <stack>
#include <iomanip>
#include <numeric>
#include <queue>
#include <climits>
#include <complex>
#include <cmath>
#include <map>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 1LL << 59

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //cout << fixed << setprecision(3);

    int n;
    cin >> n;
    while (n--)
    {
        string s;
        cin >> s;
        sort(s.begin(), s.end());
        int maxV = 0, minV = 0;
        int t = 1;
        int siz = s.size();

        for (int i = 0; i < siz; ++i)
        {
            
            minV = minV * 10 + s[i] - '0';
        }

        reverse(s.begin(), s.end());

        for (int i = 0; i < siz; ++i)
        {
            maxV = maxV * 10 + s[i] - '0';
        }
        cout << maxV - minV << endl;
    }
    return 0;
}
