//pb_ds NOI template 20150710
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <cmath>
#include <vector>
#include <set>
#include <utility>
#include <queue>
#define For(i, a, b) for(register int i = a, ___u = b; i <= ___u; ++i)
#define ForDown(i, a, b) for(register int i = b, ___d = a; i >= ___d; --i)
#define Iter(iter, vec) for(RG typeof(vec.begin()) iter = vec.begin(), __end = vec.end(); iter != __end; ++iter)
#define cmax(i, j) ((i) < (j) ? (i) = (j) : (i))
#define cmin(i, j) ((i) > (j) ? (i) = (j) : (i))
#define dmax(i, j) ((j) < (i) ? (i) : (j))
#define dmin(i, j) ((i) < (j) ? (i) : (j))
#define ddel(i, j) ((i) > (j) ? (i) - (j) : (j) - (i))
#define dabs(i) ((i) > 0 ? (i) : -(i))
#define dsqy(x) ((x) * (x))
#define lowbit(i) ((i) & -(i))

#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>

namespace io
{
	const int MAXBUF = 1 << 22;
	char B[MAXBUF], *S = B, *T = B;
	#define getc() (S == T && (T = (S = B) + fread(B, 1, MAXBUF, stdin), S == T) ? 0 : *S++)
	#define fastcall __attribute__((optimize("-O3")))
	template<class Type> inline Type read()
	{
		register Type aa = 0;
		register bool bb = 0;
		register char ch, *S = io::S, *T = io::T;
		for(ch = getc(); (ch < '0' || ch > '9') && ch != '-'; ch = getc())
			;
		for(ch == '-' ? bb = 1 : aa = ch - '0', ch = getc(); '0' <= ch && ch <= '9'; ch = getc())
			aa += (aa << 3) + aa + ch - '0';
		io::S = S, io::T = T;
		return bb ? -aa : aa; 
	}
	int (*F)() = read<int>;
	
	template<> inline double read()
	{
		register double aa = 0, bb;
		register bool cc = 0;
		register char ch;
		register char *S = io::S, *T = io::T;
		for(ch = getc(); (ch < '0' || ch > '9') && ch != '-'; ch = getc())
			;
		for(ch == '-' ? cc = 1 : aa = ch - '0', ch = getc(); '0' <= ch && ch <= '9'; ch = getc())
			aa = aa * 10 + ch - '0';
		if(ch == '.')
		{
			bb = 1;
			while(ch = getc(), ch >= '0' && ch <= '9')
				aa += (bb *= 0.1) * (ch - '0');
		}
		io::S = S, io::T = T;
		return cc ? -aa : aa;
	}
	
	char buff[MAXBUF], *iter = buff;
	template<class T>inline void P(register T x, register char ch = '\n')
	{
		static int stack[110];
		register int O = 0;
		register char *iter = io::iter;
		if(!x)*iter++ = '0';
		else
		{
			(x < 0) ? x = -x, *iter++ = '-' : 1;
			for(; x; x /= 10)
				stack[++O] = x % 10;
			for(; O; *iter++ = '0' + stack[O--])
				;
		}
		*iter++ = ch, io::iter = iter;
	}

	inline int gets(register char *o)
	{
		register char *s = o, ch = getc();
		register char *S = io::S, *T = io::T;
		for(; ch == ' ' || ch == '\n' || ch == '\r' || !ch; ch = getc())
			;
		for(; ch != ' ' && ch != '\n' && ch != '\r' && ch; ch = getc())
			*s++ = ch;
		*s = 0;
		io::S = S, io::T = T;
		return s - o;
	}

	inline char get_a()
	{
		register char ch = getc();
		while(ch < 'a' || 'z' < ch) ch = getc();
		//puts("1");
		return ch;
	}
	
	inline void putc(register char ch) {*iter++ = ch;}
	
	inline void output() {fwrite(buff, 1, iter - buff, stdout), iter = buff;}
}


#define RG register
#define set_file(FILE) freopen(FILE ".in", "r", stdin), freopen(FILE ".out", "w", stdout)
#define close_file() fclose(stdin), fclose(stdout)
//#include <ext/pb_ds/priority_queue.hpp>
//#include <stdarg.h>


namespace pb_ds
{
	using io::P;	
	
	struct From {int pos, set;};
	From f[30][20];
	int g[800];
	
	inline void main()
	{
		RG int (*F)() = io::F;
		RG long long (*Fll)() = io::read<long long>;
		For(_s, 0, 728)
		{
			RG int last = 3;
			for(RG int set = _s; set; set /= 3)
			{
				if(last < set % 3) goto fail;
				last = set % 3;
			}
			g[++g[0]] = _s;
///*lgg*/printf("g %d = %d\n", g[0], _s);
			fail:;
		}
		For(__, 1, F())
		{
			memset(f, -1, sizeof f);
			
			RG long long x = Fll();
			f[0][0] = (From) {0, 0};
			RG int i;
			for(i = 0; x; ++i, x /= 10)
			{
				RG const int temp = x % 10;
///*lgg*/printf("i = %d temp = %d\n", i, temp);
				For(j, 0, 15) if(f[i][j].set != -1)
				{
///*lgg*/printf("j = %d\n", j);
					For(__s, 0, g[0])
					{
						RG int _s = g[__s];
						RG int sum = 0;
						for(RG int set = _s; set; set /= 3)
						{
							RG int s = set % 3;
							if(s == 1) sum += 4;
							if(s == 2) sum += 7;
						}
						if(sum % 10 + j == temp) //可以转移哦！
						{
							RG int advance = sum / 10;
							if(f[i + 1][advance].set != -1) continue;
///*lgg*/printf("set = %d sum = %d advance = %d\n", _s, sum, advance);
							f[i + 1][advance] = (From) {j, _s};
						}
					}
				}
///*lgg*/getchar();
			}
			if(f[i][0].set == -1) P(-1);
			else
			{
				std::vector<int> ans[6];
				RG int j = 0;
				while(i)
				{
					for(RG int set = f[i][j].set, k = 0; k < 6; set /= 3, ++k)
					{
						RG int s = set % 3;
						if(s == 0) ans[k].push_back(0);
						if(s == 1) ans[k].push_back(4);
						if(s == 2) ans[k].push_back(7);
					}
					j = f[i][j].pos;
					--i;
				}
				For(i, 0, 5)
				{
					RG int j = 0;
					while(!ans[i][j] && j < (int) ans[i].size()) ++j;
					if(j == (int) ans[i].size()) io::putc('0');
					else for(; j < (int) ans[i].size(); ++j) io::putc('0' + ans[i][j]);
					io::putc(' ');
				}
				io::putc('\n');
///*lgg*/fprintf(stderr, "%d\n", __);
			}
		}
	}	
}

int main()
{
	//set_file("graph");
	pb_ds::main();
	io::output();
	close_file();
}
/*
1
3 3 3
1 2
1 3
3 2

1
8 9 8
1 2
1 7
2 3
2 5
3 4
5 6
7 8
6 4
8 4

*/
