import java.io.*;
import java.util.*;

public class Test{
    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long ln[] = new long[n];
        for(int i=0;i<n;++i)
        {
            ln[i] = sc.nextLong();
        }
        long[] result = new long[n];
        for(int i=0;i<n;++i)
        {
            int[][] dp = new int[19][5];
            int[][] pre = new int[19][5];
            int[][][][] sol = new int[19][5][5][2]; //store 4's number and 7's number
            dp[0][0] = 1;
            long t = ln[i];
            for(int j=0;j<18;++j)
            {
                int end = (int )(t % 10);
                for(int c=0;c<5;++c){
                    if(dp[j][c] == 1){
                        for(int x=0;x<=6;++x)
                        {
                            for(int y=0;y<=6-x;++y)
                            {
                                int sum = 4 * x + 7 * y + c;
                                if(sum % 10 == end){
                                    dp[j+1][sum/10] = 1;
                                    sol[j][c][sum/10][0] = x;
                                    sol[j][c][sum/10][1] = y;
                                    pre[j+1][sum/10] = c;
                                }
                            }
                        }
                    }
                }
                t = t / 10;
            }
            if(dp[18][0] == 1)
            {
                long[] res = new long[6];
                int c = 0;
                int nextc = pre[18][0];
                for(int m=17;m>=0;m--)
                {
                    int num4 = sol[m][nextc][c][0];
                    int num7 = sol[m][nextc][c][1];
                    int cnt = 0;
                    for(int q=0;q<num4;++q)
                        res[cnt] = res[cnt++]+fastPow(10,m) * 4;
                    for(int p=0;p<num7;++p)
                        res[cnt] =res[cnt++]+fastPow(10,m) * 7;
                    c = nextc;
                    nextc = pre[m][nextc];
                }
                
                for(int z=0;z<6;++z){
                    System.out.print(res[z]+" ");
                    result[i] += res[z];
                }
                System.out.println();
            }
            else
                System.out.println(-1);
        }
        for(int i=0;i<n;++i)
            System.out.println(result[i]);
    }

    public static long fastPow(int i,int n)
    {
        if(n == 0)
            return 1;
        if(n == 1)
            return i;
        if(n % 2 == 0)
            return fastPow(i,n/2) * fastPow(i,n/2);
        else
            return i * fastPow(i,(n-1)/2) * fastPow(i,(n-1)/2);
    }

}
