#include <stdio.h>

#define N 8
int main(void){
    int n, brw;
    char in[N+1];
    int min[N],max[N],diff[N];
    char tmp;
    int j;
    
    scanf("%d", &n);
    for (int i=0; i<n; i++){
        scanf("%s", in);
        for (j=0; j<N-1; j++){
            for (int k=N-1; k>j; k--){
                if (in[k]<in[k-1]){
                    tmp = in[k];
                    in[k] = in[k-1];
                    in[k-1] = tmp;
                }
            }
            max[N-1-j] = min[j] = in[j] - '0';
        }
        max[0] = min[N-1]=in[N-1] - '0';
        brw=0;
        for (j=N-1; j>=0; j--){
            diff[j] = max[j]-min[j]-brw;
            brw = diff[j]<0 ? 1 : 0;
            if (diff[j]<0) diff[j]+=10;
        }
        for (j=0; j<N || diff[j]==0; j++);
        if (j==N){
            printf("0\n"); continue;
        } else {
            j++;
            for (; j<N; j++){
                printf("%d", diff[j]);
            }
        }
        printf("\n");
    }
    return 0;
}