#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/rope>
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define vec vector
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pw(x) (1LL<<x)
#define rall(x) x.rbegin(),x.rend()
#define fast_ioi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
using namespace __gnu_cxx;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
//template<clыass T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T> bool umax(T &a,T b){ return (a<b?a=b,1:0);}
template<typename T> bool umin(T &a,T b){ return (a>b?a=b,1:0);}
//auto rnd=bind(uniform_int_distribution<ull>(1,1e18),mt19937(time(0)));
const int N=20;
int p3[N];
int dp[2][4*N];
pii p[N][4*N];
int sm[729];
int w[3];
vec<int>bits[729];
/// index,carry,{fromcarry,bymask}
signed main(){
    fast_ioi;
    p3[0]=1;
    for(int i=1;i<7;i++){
        p3[i]=p3[i-1]*3;
    }
    w[0]=0;w[1]=4;w[2]=7;
    for(int i=0;i<729;i++){
        int fe=i;
        while(fe){
            bits[i].pb(fe%3);
            fe/=3;
        }
        while(sz(bits[i])<6) bits[i].pb(0);
        assert(sz(bits[i])==6);
        for(int j=0;j<6;j++){
            sm[i]+=w[bits[i][j]];
        }
    }
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        for(int j=0;j<4*N;j++){
            dp[0][j]=0;
        }
        dp[0][0]=1;
        string s=to_string(n);
        reverse(all(s));s+='0';
        int dl=sz(s);
        for(int i=0;i<sz(s);i++){
            int me=i%2;
            int h=me^1;
            for(int c=0;c<4*N;c++){
                dp[h][c]=0;
            }
            for(int c=0;c<4*N;c++){
                if(!dp[me][c])
                    continue;
                for(int msk=0;msk<729;msk++){
                    int nv=sm[msk]+c;
//                    debug()<<imie(nv);
//                    if(i==1 && c==4 && nv/10==0)cerr<<i<<' '<<c<<' '<<nv<<' '<<msk<<endl;
                    if(nv%10==s[i]-'0'){
                        if(umax(dp[h][nv/10],dp[me][c])){
                            p[i+1][nv/10]={c,msk};
                        }
                    }
                }
            }
        }
        if(!dp[dl%2][0]){
            cout<<-1<<endl;
        }
        else{
            int my=0;
            int i=dl;
            vec<vec<int>>vl(6,vec<int>());
            while(i){
                pii to=p[i][my];
//                cerr<<" EA "<<to.f<<' '<<to.s<<endl;
                for(int i=0;i<6;i++){
//                    cerr<<w[bits[to.f][i]]<<' '<<bits[to.s][i]<<endl;
                    vl[i].pb(w[bits[to.s][i]]);
                }
                my=to.f;
                i--;
            }
            for(auto &z : vl){
//                for(auto &q : z)
//                    cout<<"FA"<<q;
//                cout<<"W"<<endl;
                reverse(all(z));
                while(z.back()==0 && sz(z)>1)
                    z.pop_back();
                reverse(all(z));
                for(auto &q : z){
                    cout<<q;
                }
                cout<<' ';
            }
            cout<<endl;
        }
    }
    return 0;
}
/*




*/
