#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include <vector>
#include <map>

using namespace std;

void Solution()
{
    
    int n;
    
    char a[10];
    int b;
    int c;
    
    cin>>n;
    
    while (n--)
    { 
        
        
        for (int i = 0; i < 8; i++)
        {
            cin>>a[i];
        }
        
        sort(a, a + 8);
        b = atoi(a); //string to int
        
        reverse(a, a + 8);
        c = atoi(a);
        
        cout<<c - b<<endl;
    }
    
}

int main()
{
    Solution();
    return 0;
}