//llya是一个狮子！
//IT信息技术考试！
//queries请求访问！
//以帮助为借口，让程序员解决这个问题，这只是建立解决这个问题的必要性！
//问题越重要越需要解决！
//想法一，就是实现，但是复杂度将是10^10级别。
//想法二，建立一个数组，从后往前计算相同项，
//然后从前往后，只要统计数大于二，就说明后面有相同项！
//我的程序越来越有创造性了，加油！
//query片段！
/*interface*/
/*interface implementation*/
/*client*/
#include<stdio.h>
#include<string.h>
#define N 100000+1
int main() {
    char s[N];
    int count[N];
    scanf("%s",s);
    int n=strlen(s);
    int i;
    for(i=0; i<n; i++) {
        count[i]=0;
    }
    for(i=0; i<n-1; i++) {
        if(s[i]==s[i+1]) count[i+1]=count[i]+1;
	else count[i+1]=count[i];
    }
    /*
    for(i=0; i<n; i++) {
        printf("%d",count[i]);
    }
    printf("\n");
    */

    int m;
    scanf("%d",&m);
    for(i=0; i<m; i++) {//10^5
        int l,r;
        scanf("%d%d",&l,&r);
        l--;
        r--;
        //保证l，r是从零开始的！
            printf("%d\n",count[r]-count[l]);
    }

    return 0;
}


//llya是一个狮子！
//IT信息技术考试！
//queries请求访问！
//以帮助为借口，让程序员解决这个问题，这只是建立解决这个问题的必要性！
//问题越重要越需要解决！
//想法一，就是实现，但是复杂度将是10^10级别。
//想法二，建立一个数组，从后往前计算相同项，
//然后从前往后，只要统计数大于二，就说明后面有相同项！
//我的程序越来越有创造性了，加油！
//query片段！
/*interface*/
/*interface implementation*/
/*client*/
#include<stdio.h>
#define N 100000+1
int main() {
    char s[N];
    scanf("%s",s);
    int m;
    scanf("%d",&m);
    int i;
    for(i=0; i<m; i++) {//10^5
        int l,r;
        scanf("%d%d",&l,&r);
	l--;
	r--;
	//保证l，r是从零开始的！
        int j;
        int counter=0;
        for(j=l; j<r; j++) {//10^5
            if(s[j]==s[j+1]) {
                counter++;
            }
        }
        printf("%d\n",counter);
    }

    return 0;
}


#include<stdio.h>
#include<string.h>
char c[100005];
int main()
{
	scanf("%s",c);
	int l1=strlen(c);
	int n;
	scanf("%d",&n);
	int i,j;
	int a,b;
	int ans;
	for(i=0;i<n;i++)
	{
		scanf("%d%d",&a,&b);
	//	printf("%d %d\n",a,b);
		ans=0;
		for(j=a-1;j<b-1;j++)
		{
			if(c[j]==c[j+1])
			{
				ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char arr[100001];
	int dp[100001];
	int i,n,q,l,r;
	scanf("%s",arr);n=strlen(arr);
	for(i=0;i<n;i++)dp[i]=0;
	for(i=0;i<n-1;i++)
	{
		if(arr[i]==arr[i+1])
		{
			dp[i+1]=dp[i]+1;
		}
		else dp[i+1]=dp[i];
	}
	scanf("%d",&q);
	for(i=0;i<q;i++)
	{
		scanf("%d %d",&l,&r);
		if(n==2&&arr[0]==arr[1])printf("1\n");
		else printf("%d\n",dp[r-1]-dp[l-1]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n,i,query[10000][2],j;
	int construct[10000];
	char arr[10000];
	scanf("%s",arr);
	scanf("%d",&n);
	for(i=0;i<n;i++)scanf("%d%d",&query[i][0],&query[i][1]);
	int len = strlen(arr);
	for(i=0;i<len-1;i++)
	{
		if(arr[i]==arr[i+1])construct[i]=1;
		else construct[i]=0;
	}
	construct[len-1]=0;
	for(i=0;i<n;i++)
	{
		int sum = 0;
		for(j=query[i][0]-1;j<query[i][1]-1;j++)sum+=construct[j];
		printf("%d\n",sum);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char s[100001];

    scanf("%[^\n]", s);
    int n;
    n = strlen(s);
    int Sum[n], i;
    for(i=0; i<=n; i++){
        Sum[i] = 0;
    }
    int racha = 0;
    if(s[0]==s[1]){
        Sum[0] = 1;
        racha = 1;
    }

    for(i=1; i<n-1; i++){
        if(s[i]==s[i+1]){
            if(racha){
                Sum[i]=Sum[i-1]+1;
            }else{
                Sum[i]=1;
            }
            racha = 1;
        }else{
            racha = 0;
            Sum[i]=Sum[i-1];
        }
    }
    Sum[n-1]=Sum[n-2]+1;
    int m, l, r;
    scanf("%d",& m);
    for(i=0; i<m; i++){
        scanf("%d %d", &l, &r);
        printf("%d\n", Sum[r-1]-Sum[l-1]);
    }

    return 0;
}


#include<stdio.h>
int max(int a,int b)
{
     if(a>b)
	  return a;
     return b;
}
int main()
{
     char s[100009];
     scanf("%s",s);
     int n,temp,l,r,ans[100009],i;
     ans[0]=0;
     for(i=0;s[i+1]!='\0';i++)
	  if(s[i]==s[i+1])
	       ans[i+1]=ans[i]+1;
	  else
	       ans[i+1]=ans[i];
     scanf("%d",&n);
     while(n--)
     {
     scanf("%d%d",&l,&r);
     l--;r--;
     printf("%d\n",ans[r]-ans[l]);
     }
     for(i=0;i<7;i++)
	  printf("%d ",ans[i]);
     printf("\n");
     return 0;
}


#include <stdio.h>

int main ()
{
	int m,i;
	char str[100000];
	scanf("%[^\t\n]s",str);
	scanf("%d", &m);

	int l[m],r[m];
	for (i=0;i<m;i++)
	{
		scanf("%d", &l[i]);
		scanf("%d", &r[i]);
	}

	// printf("%s\n", str);
	int length = strlen(str);
	int result[length];
	result[0]=0;
	for (i=0;i<length;i++)
	{
		int count=0;
		if (str[i]==str[i+1])
		{
			count = 1;
		}
		result[i+1] = result[i]+count;
		// printf("%d %d\n", result[i+1], i+1);
	}
	for(i=0;i<m;i++)
	{
		int p, count=0;
		int a = l[i];
		int b = r[i];
		int count1 = result[b-1]-result[a-1];
		printf("%d\n", count1);
	}
	return 0;
}


#include <stdio.h>

int main ()
{
	int m,i;
	char str[100000];
	scanf("%[^\t\n]s",str);
	scanf("%d", &m);

	int l[m],r[m];
	for (i=0;i<m;i++)
	{
		scanf("%d", &l[i]);
		scanf("%d", &r[i]);
	}

	// printf("%s\n", str);
	int length = strlen(str);
	int result[m];
	result[0]=0;
	for (i=0;i<length;i++)
	{
		if (str[i]==str[i+1])
		{
			result[i+1]=1;
			// printf("%d\n",result[i+1] );
		}
		else
			result[i+1]=0;
			// printf("%d\n",result[i+1] );
	}
	// return;
	for (i=0;i<m;i++)
	{
		int p, count=0,j;
		int a = l[i];
		int b = r[i];
		for(j=a;j<b;j++)
		{
			// printf("result %d\n",result[j] );
			count = count + result[j];
		}
		printf("%d\n", count);
	}
	// for(i=0;i<m;i++)
	// {
	// 	int p, count=0;
	// 	int a = l[i];
	// 	int b = r[i];
	// 	for(p=a; p<b; p++)
	// 	{
	// 		if(str[p-1] == str[p])
	// 		{
	// 			count++;
	// 		}
	// 	}
	// 	printf("%d\n", count);
	// }
	return 0;
}


#include <stdio.h>

int main ()
{
	int m,i;
	char str[100000];
	scanf("%[^\t\n]s",str);
	scanf("%d", &m);

	int l[m],r[m];
	for (i=0;i<m;i++)
	{
		scanf("%d", &l[i]);
		scanf("%d", &r[i]);
	}

	// printf("%s\n", str);
	int length = strlen(str);
	printf("length %d\n",length );
	int result[length];
	result[0]=0;
	for (i=0;i<length;i++)
	{
		if (str[i]==str[i+1])
		{
			result[i+1]=1;
			// printf("%d\n",result[i+1] );
		}
		else
		{
			result[i+1]=0;
			// printf("%d\n",result[i+1] );
		}
	}
	// return;
	for (i=0;i<m;i++)
	{
		int p, count=0,j;
		int a = l[i];
		int b = r[i];
		for(j=a;j<b;j++)
		{
			// printf("result %d\n",result[j] );
			count = count + result[j];
		}
		printf("%d\n", count);
	}
	// for(i=0;i<m;i++)
	// {
	// 	int p, count=0;
	// 	int a = l[i];
	// 	int b = r[i];
	// 	for(p=a; p<b; p++)
	// 	{
	// 		if(str[p-1] == str[p])
	// 		{
	// 			count++;
	// 		}
	// 	}
	// 	printf("%d\n", count);
	// }
	return 0;
}


#include <stdio.h>

int main ()
{
	int m,i;
	char str[100000];
	scanf("%[^\t\n]s",str);
	scanf("%d", &m);

	int l[m],r[m];
	for (i=0;i<m;i++)
	{
		scanf("%d", &l[i]);
		scanf("%d", &r[i]);
	}

	// printf("%s\n", str);

	for(i=0;i<m;i++)
	{
		int p, count=0;
		int a = l[i];
		int b = r[i];
		for(p=a; p<b; p++)
		{
			if(str[p-1] == str[p])
			{
				count++;
			}
		}
		printf("%d\n", count);
	}
	return 0;
}


#include <stdio.h>

int main ()
{
	int m,i;
	char str[100000];
	scanf("%[^\t\n]s",str);
	scanf("%d", &m);

	int l[m],r[m];
	for (i=0;i<m;i++)
	{
		scanf("%d", &l[i]);
		scanf("%d", &r[i]);
	}

	// printf("%s\n", str);
	int length = strlen(str);
	// printf("length %d\n",length );
	int result[length+1];
	result[0]=0;
	for (i=0;i<length-1;i++)
	{
		if (str[i]==str[i+1])
		{
			result[i+1]=1;
			// printf("%d , %d\n",result[i+1], i+1);
		}
		else
		{
			result[i+1]=0;
			// printf("%d , %d\n",result[i+1], i+1);
		}
	}
	// return;
	for (i=0;i<m;i++)
	{
		int p, count=0,j;
		int a = l[i];
		int b = r[i];
		for(j=a;j<b;j++)
		{
			// printf("result %d\n",result[j] );
			count = count + result[j];
		}
		printf("%d\n", count);
	}
	// for(i=0;i<m;i++)
	// {
	// 	int p, count=0;
	// 	int a = l[i];
	// 	int b = r[i];
	// 	for(p=a; p<b; p++)
	// 	{
	// 		if(str[p-1] == str[p])
	// 		{
	// 			count++;
	// 		}
	// 	}
	// 	printf("%d\n", count);
	// }
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int len,i,c,l,r,m,k,p;
	int a[100005]={0};
	char str[100005];
	scanf("%s",str);
	len=strlen(str);
	a[len-1]=0;
	for(i=0;i<len-1;i++)
	{
		if(str[i]==str[i+1])
			a[i]=1;
	}
	for(i=1;i<len;i++)
	{
		a[i]=a[i]+a[i-1];
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&l,&r);
		if(l<2)
			k=0;
		else
			k=a[l-2];
		if(r<2)
			p=k;
		else
			p=a[r-2];

		c=p-k;
		printf("%d\n",c);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int len,i,c,l,r,m;
	char str[100005];
	scanf("%s",str);
	len=strlen(str);
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&l,&r);
		c=0;
		for(i=l-1;((i<r-1)&&(i<len-1));i++)
		{
			if(str[i]==str[i+1])
				c++;
		}
		printf("%d\n",c);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>

char c[100010];
int dy[100010];

int main ()
{
    int n,k,i,j,x,y;
    scanf ("%s",c);
    for (i=1;i<strlen(c);i++)
        {
            if (c[i]==c[i-1])
                dy[i] = dy[i-1]+1;
            else
                dy[i] = dy[i-1];
        }
    scanf ("%d",&n);
    for (i=0;i<n;i++)
        {
            scanf ("%d %d",&x,&y);
            x--; y--;
            if (x>y)
                {
                    j=x; x=y; y=j;
                }
            printf ("%d\n",dy[y]-dy[x]);
        }
    scanf (" ");
    return 0;
}


#include <stdio.h>
#include <string.h>

char c[1000000];
int dy[1000000];

int main ()
{
    int n,k,i,j,x,y;
    scanf ("%s",c);
    for (i=1;i<strlen(c);i++)
        {
            if (c[i]==c[i-1])
                dy[i] = dy[i-1]+1;
            else
                dy[i] = dy[i-1];
        }
    scanf ("%d",&n);
    for (i=0;i<n;i++)
        {
            scanf ("%d %d",&x,&y);
            x--; y--;
            if (x>y)
                {
                    j=x; x=y; y=j;
                }
            printf ("%d\n",dy[y]-dy[x]);
        }
    scanf (" ");
    return 0;
}


#include <stdio.h>
#include <string.h>

char c[100001];
int dy[100001];

int main ()
{
    int n,i,x,y;
    scanf ("%s %d",c,&n);
    for (i=1;i<strlen(c);i++)
        {
            if (c[i]==c[i-1])
                dy[i] = dy[i-1]+1;
            else
                dy[i] = dy[i-1];
        }
    for (i=0;i<n;i++)
        {
            scanf ("%d %d",&x,&y);
            printf ("%d\n",dy[y-1]-dy[x-1]);
        }
    scanf (" ");
    return 0;
}


#include <stdio.h>
#include <string.h>

char c[1000000];
int dy[1000000];

int main ()
{
    int n,k,i,j,x,y;
    scanf ("%s",c);
    for (i=1;i<strlen(c);i++)
        {
            if (c[i]==c[i-1])
                dy[i] = dy[i-1]+1;
            else
                dy[i] = dy[i-1];
        }
    scanf ("%d",&n);
    for (i=0;i<n;i++)
        {
            scanf ("%d %d",&x,&y);
            x--; y--;
            printf ("%d\n",dy[y]-dy[x]);
        }
    scanf (" ");
    return 0;
}


#include <stdio.h>
#include <string.h>

char c[1000000];
int dy[1000000];

int main ()
{
    int n,i,x,y;
    scanf ("%s %d",c,&n);
    for (i=1;i<strlen(c);i++)
        {
            if (c[i]==c[i-1])
                dy[i] = dy[i-1]+1;
            else
                dy[i] = dy[i-1];
        }
    for (i=0;i<n;i++)
        {
            scanf ("%d %d",&x,&y);
            x--; y--;
            printf ("%d\n",dy[y]-dy[x]);
        }
    scanf (" ");
    return 0;
}


#include <stdio.h>

int main()
{
	int F[100001];
	char str[100001];
	int i, m, l, r;
	char last, ch;

	F[0] = 0;
	last = getchar();
	i = 0;
	while (1)
	{
		ch = getchar();
		if (ch == '\n')
			break;
		i++;
		if (ch == last)
			F[i] = F[i - 1] + 1;
		else
			F[i] = F[i - 1];
		last = ch;
	}

	scanf("%d", &m);
	while (m--)
	{
		scanf("%d%d", &l, &r);
		printf("%d\n", F[r - 1] - F[l - 1]);
	}
	return 0;
}


#include <stdio.h>

int main()
{
	int F[100001];
	char str[100001];
	int i, m, l, r;

	gets(str);

	F[0] = 0;
	for (i = 1; i < strlen(str); i++)
		if (str[i] == str[i - 1])
			F[i] = F[i - 1] + 1;
		else
			F[i] = F[i - 1];

	scanf("%d", &m);
	while (m--)
	{
		scanf("%d%d", &l, &r);
		printf("%d\n", F[r - 1] - F[l - 1]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(scanf("%s",a)!=EOF)
    {
        scanf("%d",&n);
        if(n==0)
        break;
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            if(x==0||y==0)
            break;
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        getchar();
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int b[100000]={0};
int main()
{
    int n,i,k,sum=0,x,y,num;
    gets(a);
    k=strlen(a);
    for(i=0;i<k-1;i++)
    {
        if(a[i]==a[i+1])
        {
            sum++;
        }
        b[i+1]=sum;
    }
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        num=b[y-1]-b[x-1];
        printf("%d\n",num);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int b[100000]={0};
int main()
{
    int n,i,k,sum=0,x,y,num;
    gets(a);
    k=strlen(a);
    for(i=0;i<k-1;i++)
    {
        if(a[i]=a[i+1])
        {
            sum++;
        }
         b[i+1]=sum;
    }
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        num=b[y-1]-b[x-1];
        printf("%d\n",num);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(scanf("%s",a)!=NULL)
    {
        scanf("%d",&n);
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            if(x==0||y==0)
            break;
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        getchar();
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(1)
    {
        gets(a);
        scanf("%d",&n);
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            if(x==0||y==0)
            break;
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        getchar();
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(scanf("%s",a)!=EOF)
    {
        scanf("%d",&n);
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            if(x==0||y==0)
            break;
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        getchar();
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,k,j,sum;
    int x,y;
    gets(a);
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        sum=0;
        scanf("%d%d",&x,&y);
        for(j=x-1;j<y-1;j++)
        {
            if(a[j]==a[j+1])
            sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(scanf("%s",a)!=EOF)
    {
        scanf("%d",&n);
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            if(x==0||y==0)
            break;
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        //getchar();
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[10000000];
int main()
{
    int n,i,k,j,sum;
    int x,y;
    gets(a);
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        sum=0;
        scanf("%d%d",&x,&y);
        for(j=x-1;j<y-1;j++)
        {
            if(a[j]==a[j+1])
            sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char a[100000];
int main()
{
    int n,i,x,y,sum;
    while(scanf("%s",a)!=EOF)
    {
        scanf("%d",&n);
        while(n--)
        {
            sum=0;
            scanf("%d%d",&x,&y);
            for(i=x-1;i<y-1;i++)
            {
                if(a[i]==a[i+1])
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        getchar();
    }
    return 0;
}


#include<stdio.h>
#include<limits.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#define gc getchar//_unlocked
#define Max(a,b) (((a)>=(b))?(a):(b))
#define Min(a,b) (((a)<=(b))?(a):(b))

typedef long long ll;
typedef unsigned long long ull;

ull getnum()
{
    char ch;
    ull num=0;

    while(!isdigit(ch=gc()));

    do
    {
        num=num*10+(ch-'0');

    }while(isdigit(ch=gc()));

    return num;
}

void sort(int n,int a[2][n+1] , int begin , int mid , int end)
{
    int i=begin,j=mid+1,k=0,temp[2][end-begin+1];

    while(i<=mid && j<=end)
    {
        if(a[1][i] <= a[1][j])
        {
            temp[0][k] =a[0][i];
            temp[1][k] =a[1][i];
            ++i;
        }
        else
        {
            temp[0][k]=a[0][j];
            temp[1][k]=a[1][j];
            ++j;
        }

        ++k;
    }

    for(;i<=mid;++i,++k)
    {
        temp[0][k]=a[0][i];
        temp[1][k]=a[1][i];
    }

    for(;j<=end;++j,++k)
    {
        temp[0][k]=a[0][j];
        temp[1][k]=a[1][j];
    }

    for(i=0;i<k;++i)
    {
        a[0][begin+i]= temp[0][i];
        a[1][begin+i]= temp[1][i];
    }

}

void partition(int n,int a[2][n+1] , int begin ,int end)
{
    if(begin==end)
        return ;

    int mid= (begin+end)/2;

    partition(n,a,begin,mid);
    partition(n,a,mid+1,end);

    sort(n,a,begin,mid,end);

}

int main()
{
    char str[100001];
    int ans[100000]={0};

    scanf("%s",str);

    int m,len,count=0,l,r,i;

    len =strlen(str);

    m=getnum();

    for(i=0;i<len-1;++i)
    {
        if(str[i]==str[i+1])
            ans[i]=1;
        else
            ans[i]=0;

    }

    for(i=0;i<len;++i)
    {
        if(ans[i]==1)
            ++count;

        ans[i]=count;
    }

    while(m--)
    {
        scanf("%d%d\n",&l,&r);

        l=l-2;
        r=r-2;

        if(l>=0 && r>=0)
            printf("%d\n",ans[r]-ans[l]);
        else if(r>=0)
            printf("%d\n",ans[r]);
        else
            printf("0\n");

    }

    return 0;
}


#include<stdio.h>
#include<limits.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#define gc getchar//_unlocked
#define Max(a,b) (((a)>=(b))?(a):(b))
#define Min(a,b) (((a)<=(b))?(a):(b))

typedef long long ll;
typedef unsigned long long ull;

ull getnum()
{
    char ch;
    ull num=0;

    while(!isdigit(ch=gc()));

    do
    {
        num=num*10+(ch-'0');

    }while(isdigit(ch=gc()));

    return num;
}

void sort(int n,int a[2][n+1] , int begin , int mid , int end)
{
    int i=begin,j=mid+1,k=0,temp[2][end-begin+1];

    while(i<=mid && j<=end)
    {
        if(a[1][i] <= a[1][j])
        {
            temp[0][k] =a[0][i];
            temp[1][k] =a[1][i];
            ++i;
        }
        else
        {
            temp[0][k]=a[0][j];
            temp[1][k]=a[1][j];
            ++j;
        }

        ++k;
    }

    for(;i<=mid;++i,++k)
    {
        temp[0][k]=a[0][i];
        temp[1][k]=a[1][i];
    }

    for(;j<=end;++j,++k)
    {
        temp[0][k]=a[0][j];
        temp[1][k]=a[1][j];
    }

    for(i=0;i<k;++i)
    {
        a[0][begin+i]= temp[0][i];
        a[1][begin+i]= temp[1][i];
    }

}

void partition(int n,int a[2][n+1] , int begin ,int end)
{
    if(begin==end)
        return ;

    int mid= (begin+end)/2;

    partition(n,a,begin,mid);
    partition(n,a,mid+1,end);

    sort(n,a,begin,mid,end);

}

int main()
{
    char str[100001];
    int ans[100000]={0};

    scanf("%s",str);

    int m,len,count=0,l,r,i;

    len =strlen(str);

    m=getnum();

    for(i=0;i<len-1;++i)
    {
        if(str[i]==str[i+1])
            ans[i]=1;
        else
            ans[i]=0;

    }

    for(i=0;i<len;++i)
    {
        if(ans[i]==1)
            ++count;

        ans[i]=count;
    }

    for(i=0;i<len;++i)
        printf("%d , ", ans[i]);
    printf("\n");
    while(m--)
    {
        scanf("%d%d\n",&l,&r);

        l=l-2;
        r=r-2;

        if(l>=0 && r>=0)
            printf("%d\n",ans[r]-ans[l]);
        else if(r>=0)
            printf("%d\n",ans[r]);
        else
            printf("0\n");

    }

    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int i;
	char s[100001];
	scanf("%s",s);
	int a[strlen(s)];
	a[0]=0;
	for(i=1;i<strlen(s);i++)
	{
		if(s[i]==s[i-1])
		{
			a[i]=a[i-1]+1;
		}
		else
		{
			a[i]=a[i-1];
		}
	}
	int d;
	scanf("%d",&d);
	for(i=0;i<d;i++)
	{
		int w,e;
		scanf("%d %d",&w,&e);
		printf("%d\n",a[e-1]-a[w-1]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int i;
	char s[100001];
	scanf("%s",s);
	int a[strlen(s)];
	a[0]=0;
	for(i=0;i<strlen(s)-1;i++)
	{
		if(s[i]==s[i+1])
		{
			a[i+1]=a[i]+1;
		}
	}
	int d;
	scanf("%d",&d);
	for(i=0;i<d;i++)
	{
		int w,e;
		scanf("%d %d",&w,&e);
		printf("%d\n",a[e-1]-a[w-1]);
	}
	return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length=0,m,j,k,i,result[100001]={0};

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=length-2;i>=0;i--)
    {
     result[i]=result[i+1]+(input[i]==input[i+1]);
    }
    while(m--)
    {
      scanf("%d%d",&j,&k);
     printf("%d\n",result[j-1]-result[k-1]);
    }

return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,out;

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
      out=0;
      scanf("%d%d",&j,&k);
      for(j;j<k;j++)
        if(input[j-1]==input[j])
          out++;
       printf("%d\n",out);
    }

return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,out,result[100001];

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
      out=0;
      scanf("%d%d",&j,&k);
      for(j;j<k;j++)
        if(input[j-1]==input[j])
          out++;
      result[i]=out;
    }
    for(i=0;i<m;i++)
      printf("%d\n",result[i]);
return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,result[100001]={0};

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=length-1;i>0;i--)
    {
     result[i]=result[i+1]+(input[i-1]==input[i]);
    }
    while(m--)
    {
      scanf("%d%d",&j,&k);
     printf("%d\n",result[j]-result[k]);
    }

return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,out,result[100001];

    scanf("%s",&input);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
      out=0;
      scanf("%d%d",&j,&k);
      for(j;j<k;j++)
        if(input[j-1]==input[j])
          out++;
      result[i]=out;
    }
    for(i=0;i<m;i++)
      printf("%d\n",result[i]);
return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,result[100001]={0};

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=m-1;i>=0;i--)
    {
     result[i]=result[i+1]+(input[i]==input[i+1]);
    }
    while(m--)
    {
      scanf("%d%d",&j,&k);
     printf("%d\n",result[j]-result[k]);
    }

return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,out,result[100001];

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    i=m;
     while(i--)
    {
      out=0;
      scanf("%d%d",&j,&k);
      for(j;j<k;j++)
        if(input[j-1]==input[j])
          out++;
      result[i]=out;
    }
    i=m;
    while(i--)
      printf("%d\n",result[i]);
return 0;
}


#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[100001];
    int length,m,j,k,i,result[100001]={0};

    while((input[length++]=getchar())!='\n')
        ;
    scanf("%d",&m);
    for(i=length-2;i>=0;i--)
    {
     result[i]=result[i+1]+(input[i]==input[i+1]);
    }
    while(m--)
    {
      scanf("%d%d",&j,&k);
     printf("%d\n",result[j-1]-result[k-1]);
    }

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[100001];
int a;
scanf("%s",str);

int dp[100001];

int i;

int n;
scanf("%d",&n);

dp[0]=0;

for(i=1;i<strlen(str);i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

for(i=0;i<n;i++){
int l,r;
scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[1000001];
int a;
scanf("%s",str);

int dp[100001];

int i;

dp[0]=0;

for(i=1;i<strlen(str);i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

/*for(i=0;i<strlen(str);i++)
	printf("%d ",dp[i]);

return 0;
*/
int n;
scanf("%d",&n);
int l,r;
for(i=0;i<n;i++){

scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[100001];
int a;
scanf("%s",str);

int dp[100001];

int i;

dp[0]=0;

for(i=1;i<strlen(str);i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

/*for(i=0;i<strlen(str);i++)
	printf("%d ",dp[i]);

return 0;
*/
int n;
scanf("%d",&n);
for(i=0;i<n;i++){
int l,r;
scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[100001];
int a;
scanf("%s",str);

int dp[100001];

int i;

dp[0]=0;

for(i=1;i<strlen(str);i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

/*for(i=0;i<strlen(str);i++)
	printf("%d ",dp[i]);

return 0;
*/
int n;
scanf("%d",&n);
int l,r;
for(i=0;i<n;i++){

scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[100001];
int a;
scanf("%s",str);

int dp[100001];

int i;

int n;
scanf("%d",&n);
int lim=strlen(str);
dp[0]=0;

for(i=1;i<lim;i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

for(i=0;i<n;i++){
int l,r;
scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){

char str[100001];

scanf("%s",str);

int dp[100001];

int i;

dp[0]=0;

for(i=1;i<strlen(str);i++){

if(str[i]==str[i-1])
	dp[i]=1+dp[i-1];
else
	dp[i]=dp[i-1];
}

/*for(i=0;i<strlen(str);i++)
	printf("%d ",dp[i]);

return 0;
*/
int n;
scanf("%d",&n);
int l,r;
for(i=0;i<n;i++){

scanf("%d %d",&l,&r);

printf("%d\n",dp[r-1]-dp[l-1]);

}

return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int l,r,m;
    char n[100001];
    scanf("%s%i", &n, &m);
    int k[strlen(n)];
    k[0]=0;
    for(int i=1; i<strlen(n); i++){
        if(n[i]==n[i-1]){
            k[i]=1+k[i-1];
        }else{
            k[i]=k[i-1];
        }
    }
    for(int i=0; i<m; i++){
        scanf("%i%i", &l, &r);
        printf("%i\n", k[r-1]-k[l-1]);
    }
    return 0;
};


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int l,r,m,res;
    char n[100001];
    scanf("%s%i", &n, &m);
    for(int i=0; i<m; i++){
        scanf("%i%i", &l, &r);
        res=0;
        for(int j=l-1; j<r-1; j++){
            if(n[j]==n[j+1]){
                res++;
            }
        }
        printf("%i\n", res);
    }
    return 0;
};


#include <stdio.h>
#include <string.h>

int main()
{
	int a, b, c, d, e, f;
	char ZA[100001];
	int ZB[100001];

	scanf("%s%d", ZA, &a);
	f = strlen(ZA);
	for (b = 0; b < f - 1; ++b) {
		if (ZA[b] == ZA[b + 1])
			ZB[b] = 1;
		else
			ZB[b] = 0;
	}
	c = 0;
	for (b = 0; b < f; ++b) {
		d = ZB[b];
		ZB[b] = c;
		c += d;
	}
	for (c = 0; c < a; ++c) {
		scanf("%d%d", &d, &e);
		printf("%d\n", ZB[e - 1] - ZB[d - 1]);
	}

	return 0;
}


#include <stdio.h>

int main()
{
	int a, b, c, d, e, f;
	char ZA[100001];

	scanf("%s%d", ZA, &a);
	for (b = 0; b < a; ++b) {
		scanf("%d%d", &c, &d);
		f = 0;
		for (e = c - 1; e < d - 1; ++e)
			if (ZA[e] == ZA[e + 1])
				f += 1;
		printf("%d\n", f);
	}

	return 0;
}


#include<stdio.h>
#include<string.h>
char ch[100010];
int main()
{
	int m,l,r,len,s;
	while(~scanf("%s",ch))
	{
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d%d",&l,&r);
			s=0;
			l-=1,r-=1;
			for(;l<r;l++)
			{
				if(ch[l]==ch[l+1])
					s+=1;
			}
			printf("%d\n",s);
		}
		getchar();
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
char ch[100010];
int arr[100010];
int main()
{
	int m,l,r,len,i,s;
	while(~scanf("%s",ch))
	{
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d%d",&l,&r);
			s=0;
			l-=1,r-=1;
			len=strlen(ch);
			for(i=0;i<len-1;i++)
			{
				if(ch[i]==ch[i+1])arr[i]=1;
				else arr[i]=0;
			}
			for(;l<r;l++)s+=arr[l];
			printf("%d\n",s);
		}
		getchar();
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
char ch[10010];
int main()
{
	int m,l,r,len;
	long int s;
	while(~scanf("%s",ch))
	{
		len=strlen(ch);
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d%d",&l,&r);
			s=0;
			l-=1,r-=1;
			for(;l<r;l++)
			{
				if(ch[l]==ch[l+1])
					s+=1;
			}
			printf("%ld\n",s);
		}
		getchar();
	}
	return 0;
}


 #include <cstdio>
#include <string>
#include <sstream>
using namespace std;

typedef long long ll;

const int Maxn = 20;
const int Maxd = 10;
const int Maxr = 6;

int t;
ll n;
string s;
int dp[Maxn][Maxd], a[Maxn][Maxd], b[Maxn][Maxd];
string res[Maxr];

bool Get(int r, int c)
{
	if (r < 0) return c == 0;
	if (dp[r][c] == 0) {
		dp[r][c] = -1;
		for (int i = 0; i <= Maxr && dp[r][c] == -1; i++)
			for (int j = 0; i + j <= Maxr && dp[r][c] == -1; j++) {
				int has = c + 4 * i + 7 * j;
				if (has % 10 + '0' == s[r] && Get(r - 1, has / 10)) {
					dp[r][c] = 1; a[r][c] = i; b[r][c] = j;
				}
			}
	}
	return dp[r][c] == 1;
}

int main()
{
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &n);
		stringstream ss; ss << n;
		ss >> s;
		for (int i = 0; i < s.length(); i++)
			for (int j = 0; j < Maxd; j++)
				dp[i][j] = 0;
		if (Get(s.length() - 1, 0)) {
			for (int i = 0; i < Maxr; i++)
				res[i] = "";
			int r = s.length() - 1, c = 0;
			while (r >= 0) {
				int ind = 0;
				for (int i = 0; i < a[r][c]; i++) { res[ind] = "4" + res[ind]; ind++; }
				for (int i = 0; i < b[r][c]; i++) { res[ind] = "7" + res[ind]; ind++; }
				while (ind < Maxr) { res[ind] = "0" + res[ind]; ind++; }
				c = (c + a[r][c] * 4 + b[r][c] * 7) / 10; r--;
			}
			for (int i = 0; i < Maxr; i++) {
				stringstream ss(res[i]);
				ll num; ss >> num;
				printf("%I64d%c", num, i + 1 < Maxr? ' ': '\n');
			}
		} else printf("-1\n");
	}
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define REPP(i,l,r,c) for (int i=l;i<r;i+=c)
#define REP(i,l,r) REPP(i,l,r,1)
#define REPS(i,l,r) REP(i,l,r+1)
#define FOR(i,n) REP(i,0,n)
#define REPD(i,l,r) for (int i=r-1;i>=l;i--)
#define FORD(i,n) REPD(i,0,n)
#define FOREACH(i,n) for (auto i:n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int INF=(int)1E9;
const double INFD=1E9;
const ll INFLL=(ll)1E15;
const ll MOD=(ll)1E9+7;
const long double PI=acos(-1);
const long double EPS=1E-9;

bool between(int x,int l,int r) {
	return (l<=x && x<=r);
}

string tostring(ll x) {
	char dum[20]; sprintf(dum,"%I64d",x);
	string ret(dum); return ret;
}

vector<int> V[50];
int C[]={0,4,7};
ll n; string S;
ll ans[6];

void bf(int cur,int carry, ll ten, bool &done) {
//	cout<<cur<<" "<<carry<<" "<<ten<<endl;
	if (cur<0) {
		if (carry==0) done=true; return;
	}
	if (done) return;
	int digit=S[cur]-'0'; digit-=carry;
	if (cur==0 && digit==0) {
		done=true; return;
	}
	carry=0; while (digit<0) digit+=10, carry++;
	for (int i=digit;i<=42;i+=10,carry++) {
		if (V[i].empty()) continue;
//		cout<<">>"<<i<<endl;
		FOR(j,6) ans[j]+=(ll)V[i][j]*ten;
		bf(cur-1,carry,ten*10,done);
		if (done) return;
		FOR(j,6) ans[j]-=(ll)V[i][j]*ten;
	}
}

inline void work(int kasus) {
	scanf("%I64d",&n); S=tostring(n);
	cout<<S<<endl;
	bool ada=false; memset(ans,0,sizeof ans);
	bf(S.size()-1,0,1,ada);
	if (ada) {
		FOR(i,6) {
			if (i!=0) printf(" "); printf("%I64d",ans[i]);
		} puts("");
	}
	else puts("-1");

//	printf("Case #%d:",kasus);
}

inline void haram() {
	V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[4].pb(4);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[7].pb(7);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[8].pb(4);V[8].pb(4);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[11].pb(7);V[11].pb(4);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[12].pb(4);V[12].pb(4);V[12].pb(4);V[12].pb(0);V[12].pb(0);V[12].pb(0);V[14].pb(7);V[14].pb(7);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[15].pb(7);V[15].pb(4);V[15].pb(4);V[15].pb(0);V[15].pb(0);V[15].pb(0);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(0);V[16].pb(0);V[18].pb(7);V[18].pb(7);V[18].pb(4);V[18].pb(0);V[18].pb(0);V[18].pb(0);V[19].pb(7);V[19].pb(4);V[19].pb(4);V[19].pb(4);V[19].pb(0);V[19].pb(0);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(0);V[21].pb(7);V[21].pb(7);V[21].pb(7);V[21].pb(0);V[21].pb(0);V[21].pb(0);V[22].pb(7);V[22].pb(7);V[22].pb(4);V[22].pb(4);V[22].pb(0);V[22].pb(0);V[23].pb(7);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(0);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[25].pb(7);V[25].pb(7);V[25].pb(7);V[25].pb(4);V[25].pb(0);V[25].pb(0);V[26].pb(7);V[26].pb(7);V[26].pb(4);V[26].pb(4);V[26].pb(4);V[26].pb(0);V[27].pb(7);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(0);V[28].pb(0);V[29].pb(7);V[29].pb(7);V[29].pb(7);V[29].pb(4);V[29].pb(4);V[29].pb(0);V[30].pb(7);V[30].pb(7);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(4);V[32].pb(0);V[33].pb(7);V[33].pb(7);V[33].pb(7);V[33].pb(4);V[33].pb(4);V[33].pb(4);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(0);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(4);V[36].pb(4);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(4);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);
}

int main() {
	haram();
	int kasus; scanf("%d",&kasus); REPS(ik,1,kasus) work(ik);

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define REPP(i,l,r,c) for (int i=l;i<r;i+=c)
#define REP(i,l,r) REPP(i,l,r,1)
#define REPS(i,l,r) REP(i,l,r+1)
#define FOR(i,n) REP(i,0,n)
#define REPD(i,l,r) for (int i=r-1;i>=l;i--)
#define FORD(i,n) REPD(i,0,n)
#define FOREACH(i,n) for (auto i:n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int INF=(int)1E9;
const double INFD=1E9;
const ll INFLL=(ll)1E15;
const ll MOD=(ll)1E9+7;
const long double PI=acos(-1);
const long double EPS=1E-9;

bool between(int x,int l,int r) {
	return (l<=x && x<=r);
}

string tostring(ll x) {
	char dum[20]; sprintf(dum,"%I64d",x);
	string ret(dum); return ret;
}

vector<int> V[50];
int C[]={0,4,7};
ll n; string S;
ll ans[6];

void bf(int cur,int carry, ll ten, bool &done) {
//	cout<<cur<<" "<<carry<<" "<<ten<<endl;
	if (cur<0) {
		if (carry==0) done=true; return;
	}
	if (done) return;
	int digit=S[cur]-'0'; digit-=carry;
	if (cur==0 && digit==0) {
		done=true; return;
	}
	carry=0; while (digit<0) digit+=10, carry++;
	for (int i=digit;i<=42;i+=10,carry++) {
		if (V[i].empty()) continue;
//		cout<<">>"<<i<<endl;
		FOR(j,6) ans[j]+=(ll)V[i][j]*ten;
		bf(cur-1,carry,ten*10,done);
		if (done) return;
		FOR(j,6) ans[j]-=(ll)V[i][j]*ten;
	}
}

inline void work(int kasus) {
	scanf("%I64d",&n); S=tostring(n);
//	cout<<S<<endl;
	bool ada=false; memset(ans,0,sizeof ans);
	bf(S.size()-1,0,1,ada);
	if (ada) {
		FOR(i,6) {
			if (i!=0) printf(" "); printf("%I64d",ans[i]);
		} puts("");
	}
	else puts("-1");

//	printf("Case #%d:",kasus);
}

inline void haram() {
	V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[4].pb(4);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[7].pb(7);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[8].pb(4);V[8].pb(4);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[11].pb(7);V[11].pb(4);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[12].pb(4);V[12].pb(4);V[12].pb(4);V[12].pb(0);V[12].pb(0);V[12].pb(0);V[14].pb(7);V[14].pb(7);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[15].pb(7);V[15].pb(4);V[15].pb(4);V[15].pb(0);V[15].pb(0);V[15].pb(0);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(0);V[16].pb(0);V[18].pb(7);V[18].pb(7);V[18].pb(4);V[18].pb(0);V[18].pb(0);V[18].pb(0);V[19].pb(7);V[19].pb(4);V[19].pb(4);V[19].pb(4);V[19].pb(0);V[19].pb(0);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(0);V[21].pb(7);V[21].pb(7);V[21].pb(7);V[21].pb(0);V[21].pb(0);V[21].pb(0);V[22].pb(7);V[22].pb(7);V[22].pb(4);V[22].pb(4);V[22].pb(0);V[22].pb(0);V[23].pb(7);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(0);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[25].pb(7);V[25].pb(7);V[25].pb(7);V[25].pb(4);V[25].pb(0);V[25].pb(0);V[26].pb(7);V[26].pb(7);V[26].pb(4);V[26].pb(4);V[26].pb(4);V[26].pb(0);V[27].pb(7);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(0);V[28].pb(0);V[29].pb(7);V[29].pb(7);V[29].pb(7);V[29].pb(4);V[29].pb(4);V[29].pb(0);V[30].pb(7);V[30].pb(7);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(4);V[32].pb(0);V[33].pb(7);V[33].pb(7);V[33].pb(7);V[33].pb(4);V[33].pb(4);V[33].pb(4);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(0);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(4);V[36].pb(4);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(4);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);
}

int main() {
	haram();
	int kasus; scanf("%d",&kasus); REPS(ik,1,kasus) work(ik);

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define REPP(i,l,r,c) for (int i=l;i<r;i+=c)
#define REP(i,l,r) REPP(i,l,r,1)
#define REPS(i,l,r) REP(i,l,r+1)
#define FOR(i,n) REP(i,0,n)
#define REPD(i,l,r) for (int i=r-1;i>=l;i--)
#define FORD(i,n) REPD(i,0,n)
#define FOREACH(i,n) for (auto i:n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int INF=(int)1E9;
const double INFD=1E9;
const ll INFLL=(ll)1E15;
const ll MOD=(ll)1E9+7;
const long double PI=acos(-1);
const long double EPS=1E-9;

bool between(int x,int l,int r) {
	return (l<=x && x<=r);
}

string tostring(ll x) {
	char dum[20]; sprintf(dum,"%I64d",x);
	string ret(dum); return ret;
}

vector<int> V[50];
int C[]={0,4,7};
int n; string S;
ll ans[6];

void bf(int cur,int carry, ll ten, bool &done) {
//	cout<<cur<<" "<<carry<<" "<<ten<<endl;
	if (cur<0) {
		if (carry==0) done=true; return;
	}
	if (done) return;
	int digit=S[cur]-'0'; digit-=carry;
	if (cur==0 && digit==0) {
		done=true; return;
	}
	carry=0; while (digit<0) digit+=10, carry++;
	for (int i=digit;i<=42;i+=10,carry++) {
		if (V[i].empty()) continue;
//		cout<<">>"<<i<<endl;
		FOR(j,6) ans[j]+=(ll)V[i][j]*ten;
		bf(cur-1,carry,ten*10,done);
		if (done) return;
		FOR(j,6) ans[j]-=(ll)V[i][j]*ten;
	}
}

inline void work(int kasus) {
	scanf("%I64d",&n); S=tostring(n);
	bool ada=false; memset(ans,0,sizeof ans);
	bf(S.size()-1,0,1,ada);
	if (ada) {
		FOR(i,6) {
			if (i!=0) printf(" "); printf("%I64d",ans[i]);
		} puts("");
	}
	else puts("-1");

//	printf("Case #%d:",kasus);
}

inline void haram() {
	V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[0].pb(0);V[4].pb(4);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[4].pb(0);V[7].pb(7);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[7].pb(0);V[8].pb(4);V[8].pb(4);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[8].pb(0);V[11].pb(7);V[11].pb(4);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[11].pb(0);V[12].pb(4);V[12].pb(4);V[12].pb(4);V[12].pb(0);V[12].pb(0);V[12].pb(0);V[14].pb(7);V[14].pb(7);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[14].pb(0);V[15].pb(7);V[15].pb(4);V[15].pb(4);V[15].pb(0);V[15].pb(0);V[15].pb(0);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(4);V[16].pb(0);V[16].pb(0);V[18].pb(7);V[18].pb(7);V[18].pb(4);V[18].pb(0);V[18].pb(0);V[18].pb(0);V[19].pb(7);V[19].pb(4);V[19].pb(4);V[19].pb(4);V[19].pb(0);V[19].pb(0);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(4);V[20].pb(0);V[21].pb(7);V[21].pb(7);V[21].pb(7);V[21].pb(0);V[21].pb(0);V[21].pb(0);V[22].pb(7);V[22].pb(7);V[22].pb(4);V[22].pb(4);V[22].pb(0);V[22].pb(0);V[23].pb(7);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(4);V[23].pb(0);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[24].pb(4);V[25].pb(7);V[25].pb(7);V[25].pb(7);V[25].pb(4);V[25].pb(0);V[25].pb(0);V[26].pb(7);V[26].pb(7);V[26].pb(4);V[26].pb(4);V[26].pb(4);V[26].pb(0);V[27].pb(7);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[27].pb(4);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(7);V[28].pb(0);V[28].pb(0);V[29].pb(7);V[29].pb(7);V[29].pb(7);V[29].pb(4);V[29].pb(4);V[29].pb(0);V[30].pb(7);V[30].pb(7);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[30].pb(4);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(7);V[32].pb(4);V[32].pb(0);V[33].pb(7);V[33].pb(7);V[33].pb(7);V[33].pb(4);V[33].pb(4);V[33].pb(4);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(7);V[35].pb(0);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(7);V[36].pb(4);V[36].pb(4);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(7);V[39].pb(4);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);V[42].pb(7);
}

int main() {
	haram();
	int kasus; scanf("%d",&kasus); REPS(ik,1,kasus) work(ik);

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;

int n, L[3] = {0, 4, 7}, ok[1000];

string s;

vector<int> caras[1000];

vector< int > somas;

int dp[20][100], prox[20][100];

int solve(int i, int sobra)
{
	if(i < 0) return (sobra == 0);

	if(dp[i][sobra] != -1) return dp[i][sobra];

	int ans = 0;

	for(int j = 0; j < somas.size(); j++)
	{
		int S = somas[j] + sobra;

		int sobra2 = S/10;

		S = S - 10*sobra2;

		if(S == (s[i] - '0'))
		{
			ans |= solve(i - 1, sobra2);

			if(ans)
			{
				prox[i][sobra] = j;

				return dp[i][sobra] = 1;
			}
		}
	}

	return dp[i][sobra] = ans;
}

string ans[10];

void get(int i, int sobra)
{
	if(i < 0) return;

	int j = prox[i][sobra];

	int S = somas[j] + sobra;

	int sobra2 = S/10;

	for(int z = 0; z < caras[somas[j]].size(); z++) ans[z].push_back((char)(L[caras[somas[j]][z]] + '0'));

	get(i - 1, sobra2);
}

void build()
{
	for(int a = 0; a < 3; a++)
	for(int b = 0; b < 3; b++)
	for(int c = 0; c < 3; c++)
	for(int d = 0; d < 3; d++)
	for(int e = 0; e < 3; e++)
	for(int f = 0; f < 3; f++)
	{
		int sum = L[a] + L[b] + L[c] + L[d] + L[e] + L[f];

		if(ok[sum]) continue;

		ok[sum] = 1;

		caras[sum].push_back(a);
		caras[sum].push_back(b);
		caras[sum].push_back(c);
		caras[sum].push_back(d);
		caras[sum].push_back(e);
		caras[sum].push_back(f);

		somas.push_back(sum);
	}
}

int T;

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	build();

	cin>>T;

	while(T--)
	{
		cin>>s;

		memset(dp, -1, sizeof dp);

		for(int i = 0; i < 6; i++) ans[i].clear();

		if(solve((int)s.size() - 1, 0))
		{
			get((int)s.size() - 1, 0);

			for(int j = 0; j < 6; j++)
			{
				reverse(ans[j].begin(), ans[j].end());

				int z = 0;

				while(z < ans[j].size() and ans[j][z] == '0') z++;

				if(z >= ans[j].size()) z --;

				for(int u = z; u < ans[j].size(); u++) cout<<ans[j][u];

				cout<<" ";
			}

			cout<<"\n";
		}

		else cout<<"-1\n";
	}
}


//By Boosin_nth
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6+5;

string Number;
int test;
int check[200], Digit4[200], Digit7[200], f[20][20], Digit[20][20], Value[20][20];
long long ans[10];

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    for (int i=0; i<=6; i++)
        for (int j=0; i+j <=6; j++)
        {
            check[i*4 + j*7] = 1;
            Digit4[i*4 + j*7] = i;
            Digit7[i*4 + j*7] = j;
        }
    cin >> test;
    while (test)
    {
        test--;
        cin >> Number;
        int len_Number = Number.length();
        Number = ' ' + Number;
        for (int i = 19; i>=0; i--)
            for (int j=19; j>=0; j--)
                f[i][j] = 0;
        f[len_Number][0] = 1;
        for (int i=len_Number; i>=1; i--)
        {
            int digit = Number[i] - '0';
            for (int j=0; j<=9; j++)
                if (f[i][j] == 1)
                {
                    for (int k= digit-j; k <= 42; k+=10)
                        if (check[k] == 1 and k >= 0)
                        {
                            f[i-1][(k + j) / 10] = 1;
                            Digit[i-1][(k + j) / 10] = j;
                            Value[i-1][(k + j) / 10] = k;
                        }
                }
        }
        if (f[0][0] == 0)
        {
            cout << "-1\n";
        }
        else
        {
            for (int i=1; i<=6; i++) ans[i] = 0;
            int j = 0;
            for (int i=0; i<len_Number; i++)
            {
                int Num = Value[i][j];
                for (int k=1; k<=Digit4[Num]; k++) ans[k] = ans[k] * 10 + 4;
                for (int k=1; k<=Digit7[Num]; k++) ans[k + Digit4[Num]] = ans[k + Digit4[Num]] * 10 + 7;
                for (int k = Digit4[Num] + Digit7[Num] + 1; k<=6; k++) ans[k] *= 10;
                j = Digit[i][j];
            }
            for (int i=1; i<=6; i++)
                cout << ans[i] << ' ';
            cout << "\n";
        }
    }
    return 0;
}


//By Boosin_nth
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6+5;

string Number;
int test;
int check[200], Digit4[200], Digit7[200], f[20][20];

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    for (int i=0; i<=6; i++)
        for (int j=0; i+j <=6; j++)
        {
            check[i*4 + j*7] = 1;
            Digit4[i*4 + j*7] = i;
            Digit7[i*4 + j*7] = j;
        }
    cin >> test;
    while (test)
    {
        test--;
        cin >> Number;
        int len_Number = Number.length();
        cout << len_Number << '\n';
        Number = ' ' + Number;
        for (int i = 20; i>=0; i--)
            for (int j=20; j>=0; j--)
                f[i][j] = 0;
        f[len_Number][0] = 1;
        for (int i=len_Number; i>=1; i--)
        {
            int digit = Number[i] - '0';
            for (int j=0; j<=9; j++)
                if (f[i][j])
                {
                    for (int k= digit-j; k <= 42; k+=10)
                        if (check[k] and k >= 0)
                        {
                            f[i-1][(k + j) / 10] = 1;
                        }
                }
        }
    }
    return 0;
}


//*

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long int ul;
ul luckies[] = { 0,7,4 };

ul sum(ul nbs[6]) {
	return nbs[0] + nbs[1] + nbs[2] + nbs[3] + nbs[4] + nbs[5];
}

bool expandLuck(ul nbs[6], int luckIndex, ul& candidate, int index, ul pow10, int& maxIndex) {
	for (ul& lucky : luckies) {
		nbs[luckIndex] += pow10 * lucky;
		if (luckIndex == 5) {
			if (index == maxIndex) {
				if (sum(nbs) == candidate) {
					printf("%d %d %d %d %d %d\n", nbs[0], nbs[1], nbs[2], nbs[3], nbs[4], nbs[5]);
					return true;
				}
			}
			else {
				if(sum(nbs) % pow10*10 == candidate % pow10*10)
					if (expandLuck(nbs, 0, candidate,index + 1, pow10 * 10, maxIndex))
						return true;
			}
		}
		else {
			if (expandLuck(nbs, luckIndex + 1, candidate, index, pow10, maxIndex))
				return true;
		}
		nbs[luckIndex] -= pow10 * lucky;
	}
	return false;
}

ul nbtemp[6];

void findLuckySum() {
	fill_n(nbtemp, 6, 0);
	ul candidate;
	scanf("%lu", &candidate);
	int index = 1;
	ul pow10 = 10;
	while (candidate >= pow10) {
		index++;
		pow10 *= 10;
	}
	if (!expandLuck(nbtemp, 0, candidate, 1, 1, index)) {
		printf("-1\n");
	}
}

int main() {
	int testCases;
	scanf("%d", &testCases);
	for (int i = 0; i < testCases; i++)
		findLuckySum();
	return 0;
}

//*/


//*

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long ul;

struct digits {
	short nb4;
	short nb7;
	short getNb0() {
		return 6 - nb7 - nb4;
	}
};

bool canHave[20][10];
digits path[20][6];
short origine[20][6];

int main() {
	int testCases;
	scanf("%d", &testCases);
	for (int k = 0; k < testCases; k++) {
		fill_n(&canHave[0][0], 20 * 10, false);
		ul candidate;
		scanf("%lu", &candidate);

		canHave[0][0] = true;
		for (int digit = 1; digit < 20; digit++) {
			short candNum = candidate % 10;
			candidate /= 10;

			for (short nb4 = 0; nb4 <= 6; nb4++) {
				for (short nb7 = 0; nb4 + nb7 <= 6; nb7++) {
					short res = nb4 * 4 + nb7 * 7;
					short lastRem, newDigit;
					if (res % 10 > candNum) {
						lastRem = (10 + candNum) - (res % 10);
						newDigit = (res / 10) + 1;
					}
					else {
						lastRem = candNum - (res % 10);
						newDigit = res / 10;
					}
					if (canHave[digit-1][lastRem] && !canHave[digit][newDigit]) {
						canHave[digit][newDigit] = true;
						path[digit][newDigit] = { nb4, nb7 };
						origine[digit][newDigit] = lastRem;
					}
				}
			}
		}
		if (canHave[19][0]) {
			ul nbs[6];
			fill_n(nbs, 6, 0);
			short currRem = 0;
			for (int digit = 19; digit > 0; digit--) {
				for (int nb = 0; nb < 6; nb++)
					nbs[nb] *= 10;

				int index = 0;
				for (int i = 0; i < path[digit][currRem].nb4; i++) {
					nbs[index] += 4;
					index++;
				}
				for (int i = 0; i < path[digit][currRem].nb7; i++) {
					nbs[index] += 7;
					index++;
				}
				currRem = origine[digit][currRem];
			}
			printf("%lu %lu %lu %lu %lu %lu\n", nbs[0], nbs[1], nbs[2], nbs[3], nbs[4], nbs[5]);
		}
		else {
			printf("-1\n");
		}
	}
	return 0;
}

//*/


//*

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long int ul;
ul luckies[] = {7,0,4};

ul sum(ul nbs[6]) {
	return nbs[0] + nbs[1] + nbs[2] + nbs[3] + nbs[4] + nbs[5];
}

bool expandLuck(ul nbs[6], int luckIndex, ul& candidate, int index, ul pow10, int& maxIndex) {
	for (ul& lucky : luckies) {
		nbs[luckIndex] += pow10 * lucky;
		if (luckIndex == 5) {
			if (index == maxIndex) {
				if (sum(nbs) == candidate) {
					printf("%d %d %d %d %d %d\n", nbs[0], nbs[1], nbs[2], nbs[3], nbs[4], nbs[5]);
					return true;
				}
			}
			else {
				if(sum(nbs) % pow10*10 == candidate % pow10*10)
					if (expandLuck(nbs, 0, candidate,index + 1, pow10 * 10, maxIndex))
						return true;
			}
		}
		else {
			if (expandLuck(nbs, luckIndex + 1, candidate, index, pow10, maxIndex))
				return true;
		}
		nbs[luckIndex] -= pow10 * lucky;
	}
	return false;
}

ul nbtemp[6];

void findLuckySum() {
	fill_n(nbtemp, 6, 0);
	ul candidate;
	scanf("%lu", &candidate);
	int index = 1;
	ul pow10 = 10;
	while (candidate >= pow10) {
		index++;
		pow10 *= 10;
	}
	if (!expandLuck(nbtemp, 0, candidate, 1, 1, index)) {
		printf("-1\n");
	}
}

int main() {
	int testCases;
	scanf("%d", &testCases);
	for (int i = 0; i < testCases; i++)
		findLuckySum();
	return 0;
}

//*/


//*

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ul;

struct digits {
	short nb4;
	short nb7;
	short getNb0() {
		return 6 - nb7 - nb4;
	}
};

bool canHave[20][10];
digits path[20][6];
short origine[20][6];

int main() {
	int testCases;
	scanf("%d", &testCases);
	for (int k = 0; k < testCases; k++) {
		fill_n(&canHave[0][0], 20 * 10, false);
		ul candidate;
		scanf("%llu", &candidate);
		ul test = numeric_limits<ul>::max();

		canHave[0][0] = true;
		for (int digit = 1; digit < 20; digit++) {
			short candNum = candidate % 10;
			candidate /= 10;

			for (short nb4 = 0; nb4 <= 6; nb4++) {
				for (short nb7 = 0; nb4 + nb7 <= 6; nb7++) {
					short res = nb4 * 4 + nb7 * 7;
					short lastRem, newDigit;
					if (res % 10 > candNum) {
						lastRem = (10 + candNum) - (res % 10);
						newDigit = (res / 10) + 1;
					}
					else {
						lastRem = candNum - (res % 10);
						newDigit = res / 10;
					}
					if (canHave[digit-1][lastRem] && !canHave[digit][newDigit]) {
						canHave[digit][newDigit] = true;
						path[digit][newDigit] = { nb4, nb7 };
						origine[digit][newDigit] = lastRem;
					}
				}
			}
		}
		if (canHave[19][0]) {
			ul nbs[6];
			fill_n(nbs, 6, 0);
			short currRem = 0;
			for (int digit = 19; digit > 0; digit--) {
				for (int nb = 0; nb < 6; nb++)
					nbs[nb] *= 10;

				int index = 0;
				for (int i = 0; i < path[digit][currRem].nb4; i++) {
					nbs[index] += 4;
					index++;
				}
				for (int i = 0; i < path[digit][currRem].nb7; i++) {
					nbs[index] += 7;
					index++;
				}
				currRem = origine[digit][currRem];
			}
			printf("%llu %llu %llu %llu %llu %llu\n", nbs[0], nbs[1], nbs[2], nbs[3], nbs[4], nbs[5]);
		}
		else {
			printf("-1\n");
		}
	}
	return 0;
}

//*/


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lp(i,a,n) for(int i=a;i<=n;++i)
#define lpd(i,a,n) for(int i=a;i>=n;--i)
#define mem(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define println(a) cout <<(a) <<endl
#define sz(x) ((int)(x).size())
#define readi(x) scanf("%d",&x)
#define read2i(x,y) scanf("%d%d",&x,&y)
#define read3i(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define mod 1000000007
#define eps 1e-8
#define infi 1000000000
#define infll 1000000000000000000ll
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef set<int> si;
typedef map<int,int> mii;

const int N = 100002;
int t,n,lucky[] = {0,4,7};
char a[20];
bool dp[20][6],done;
ll tenPower[20],ans[7];

void solve(int i, int c){
    if(done) return;
    if(i == -1){
        done = !c;
        return;
    }
    if(dp[i][c]) return;
    dp[i][c] = true;

    lp(q,0,2) lp(w,0,2) lp(e,0,2) lp(r,0,2) lp(t,0,2) lp(y,0,2){
        int x = lucky[q] + lucky[w] + lucky[e] + lucky[r] + lucky[t] + lucky[y] + c;
        if(x%10 == a[i] - '0'){
            ans[1] += lucky[q] * tenPower[n-i-1];
            ans[2] += lucky[w] * tenPower[n-i-1];
            ans[3] += lucky[e] * tenPower[n-i-1];
            ans[4] += lucky[r] * tenPower[n-i-1];
            ans[5] += lucky[t] * tenPower[n-i-1];
            ans[6] += lucky[y] * tenPower[n-i-1];
            solve(i-1, x/10);
            if(done) return;
            ans[1] -= lucky[q] * tenPower[n-i-1];
            ans[2] -= lucky[w] * tenPower[n-i-1];
            ans[3] -= lucky[e] * tenPower[n-i-1];
            ans[4] -= lucky[r] * tenPower[n-i-1];
            ans[5] -= lucky[t] * tenPower[n-i-1];
            ans[6] -= lucky[y] * tenPower[n-i-1];
        }
    }
}

int main(){
    tenPower[0] = 1;
    lp(i,1,18) tenPower[i] = tenPower[i-1] * 10;

    readi(t);
    while(t--){
        scanf("%s",a);
        n = strlen(a);
        mem(dp,0);
        mem(ans,0);
        done = false;
        solve(n-1, 0);
        if(done) lp(i,1,6) printf("%I64d ", ans[i]);
        else printf("-1");
        puts("");
    }
}

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/


#include <bits/stdc++.h>

typedef int ll;

char memo[10][20];
ll len;
std::string num;

std::vector<std::string> ans;

ll go(const ll& co, const ll& cur) {
	if (cur < 0) return co == 0;

	char& ret = memo[co][cur];
	if (ret != -1) return ret;
	ret = 0;

	for (ll fr = 0; fr <= 6; ++fr)
		for (ll sv = 0; sv + fr <= 6; ++sv) {
			ll sum = fr * 4 + sv * 7 + co;
			ret = ret || (sum % 10 == num[cur] - '0' && go(sum / 10, cur-1));
		}
	return ret;
}

void bb(const ll& co, const ll& cur) {
	if (cur < 0) return;

	for (ll fr = 0; fr <= 6; ++fr)
		for (ll sv = 0; sv + fr <= 6; ++sv) {
			ll sum = fr * 4 + sv * 7 + co;
			if (sum % 10 == num[cur] - '0' && go(sum / 10, cur-1)) {

				ll idx = 0;
				for (ll diff = 6 - fr - sv; diff--; )
					ans[idx++].push_back('0');
				while (fr--)
					ans[idx++].push_back('4');
				while (sv--)
					ans[idx++].push_back('7');
				bb(sum / 10, cur-1);
				return ;
			}
		}
}

void one_case(void) {
	std::cin >> num;
	len = num.length();
	memset(memo, 0xff, sizeof(memo));
	if (!go(0, len-1)) {
		std::cout << "-1\n";
		return ;
	}

	ans = std::vector<std::string>(6);
	bb(0, len-1);

	for (auto& e : ans) {
		while (static_cast<ll>(e.size()) > 1 && e.back() == '0')
			e.pop_back();
		std::reverse(e.begin(), e.end());
		std::cout << e << " ";
	}
	std::cout << "\n";
}

int main(void) {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	ll t; std::cin >> t;
	while (t--)
		one_case();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

vector<int> u[50];

int t, m, f[25][50], s[25];
ll n, ans[6], pw[20];

void init() {
  int tab[] = {0, 4, 7};
  REP(i, 3) REP(j, 3) REP(k, 3) REP(x, 3) REP(y, 3) REP(z, 3) {
    int now = tab[i] + tab[j] + tab[k] + tab[x] + tab[y] + tab[z];
    if (u[now].empty()) u[now] = {tab[i], tab[j], tab[k], tab[x], tab[y], tab[z]};
  }
  pw[0] = 1;
  FOR(i, 1, 18) pw[i] = pw[i - 1] * 10;
}

void dfs(int pos, int rem) {
  int now = rem * 10 + s[pos];
  if (now > 46) return;
  REP(i, 5) {
    int need = now - i;
    if (need < 0) continue;
    if (!u[need].empty() && f[pos + 1][i] == -1) {
      f[pos + 1][i] = rem;
      if (pos < m) dfs(pos + 1, i);
    }
  }
}

int main() {
  init();
  scanf("%d", &t);
  while (t--) {
    scanf("%lld", &n);
    m = 0;
    while (n) {
      s[++m] = n % 10;
      n /= 10;
    }
    reverse(s + 1, s + m + 1);
    reset(f, -1);
    dfs(1, 0);
    if (f[m + 1][0] == -1) {
      puts("-1");
      continue;
    }
    reset(ans, 0);
    int x = 0;
    ROF(i, m + 1, 2) {
      int d = f[i][x] * 10 - x + s[i - 1];
      REP(j, 6) ans[j] += u[d][j] * pw[m + 1 - i];
      x = f[i][x];
    }
    REP(i, 6) printf("%lld ", ans[i]);
    puts("");
  }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

vector<int> u[50];

int t, m, f[20][50], s[20];
ll n, ans[6], pw[20];

void init() {
  int tab[] = {0, 4, 7};
  REP(i, 3) REP(j, 3) REP(k, 3) REP(x, 3) REP(y, 3) REP(z, 3) {
    int now = tab[i] + tab[j] + tab[k] + tab[x] + tab[y] + tab[z];
    if (u[now].empty()) u[now] = {tab[i], tab[j], tab[k], tab[x], tab[y], tab[z]};
  }
  pw[0] = 1;
  FOR(i, 1, 18) pw[i] = pw[i - 1] * 10;
}

void dfs(int pos, int rem) {
  int now = rem * 10 + s[pos];
  if (now > 46) return;
  REP(i, 5) {
    int need = now - i;
    if (need < 0) continue;
    if (!u[need].empty() && f[pos + 1][i] == -1) {
      f[pos + 1][i] = rem;
      if (pos < m) dfs(pos + 1, i);
    }
  }
}

int main() {
  init();
  scanf("%d", &t);
  while (t--) {
    scanf("%lld", &n);
    m = 0;
    while (n) {
      s[++m] = n % 10;
      n /= 10;
    }
    reverse(s + 1, s + m + 1);
    reset(f, -1);
    dfs(1, 0);
    if (f[m + 1][0] == -1) {
      puts("-1");
      continue;
    }
    reset(ans, 0);
    int x = 0;
    ROF(i, m + 1, 2) {
      int d = f[i][x] * 10 - x + s[i - 1];
      REP(j, 6) ans[j] += u[d][j] * pw[m + 1 - i];
      x = f[i][x];
    }
    REP(i, 6) printf("%lld ", ans[i]);
    puts("");
  }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

vector<int> u[50];

int t, m, f[20][50], s[20];
ll n, ans[6], pw[20];

void init() {
  int tab[] = {0, 4, 7};
  REP(i, 3) REP(j, 3) REP(k, 3) REP(x, 3) REP(y, 3) REP(z, 3) {
    int now = tab[i] + tab[j] + tab[k] + tab[x] + tab[y] + tab[z];
    if (u[now].empty()) u[now] = {tab[i], tab[j], tab[k], tab[x], tab[y], tab[z]};
  }
  pw[0] = 1;
  FOR(i, 1, 18) pw[i] = pw[i - 1] * 10;
}

void dfs(int pos, int rem) {
  int now = rem * 10 + s[pos];
  if (now > 42) return;
  REP(i, 5) {
    int need = now - i;
    if (need < 0) continue;
    if (!u[need].empty() && f[pos + 1][i] == -1) {
      f[pos + 1][i] = rem;
      if (pos < m) dfs(pos + 1, i);
    }
  }
}

int main() {
  init();
  scanf("%d", &t);
  while (t--) {
    scanf("%lld", &n);
    m = 0;
    while (n) {
      s[++m] = n % 10;
      n /= 10;
    }
    reverse(s + 1, s + m + 1);
    reset(f, -1);
    dfs(1, 0);
    if (f[m + 1][0] == -1) {
      puts("-1");
      continue;
    }
    reset(ans, 0);
    int x = 0;
    ROF(i, m + 1, 2) {
      int d = f[i][x] * 10 - x + s[i - 1];
      REP(j, 6) ans[j] += u[d][j] * pw[m + 1 - i];
      x = f[i][x];
    }
    REP(i, 6) printf("%lld ", ans[i]);
    puts("");
  }
}


/*Bismillahir Rahmanir Rahim*///{
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout;   // replace by cerr in interactive.
template<class C> static auto dud(C *x)->decltype(os << *x, 0);
template<class C> static char dud(...);
template<class C> typename enable_if<sizeof dud<C>(0)!=1, Dbg&>::type operator<<(const C &x) { os << x; return *this; }
template<class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i!=e and cur<26; i++, cur++) *this << ", "+2*(i==b) << *i;
    return *this << ']';
}
template<class C> typename enable_if<sizeof dud<C>(0)==1, Dbg&>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
}
template<class C, size_t X> typename enable_if<sizeof(C)!=1, Dbg&>::type operator <<(C (&x)[X]) {
    return dump(begin(x), end(x));
}
template<class B, class C> Dbg &operator <<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
}
template<class C, size_t Y> struct TP {
    void operator()(Dbg& os, const C &t) { TP<C, Y-1>()(os, t); os << ", " << get<Y-1>(t); }
};
template<class C> struct TP<C, 1> { void operator()(Dbg& os, const C &t) { os << get<0>(t); } };
template<class... C> Dbg& operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t); return *this << '}';
}
Dbg &operator<<(ostream&(*x)(std::ostream&)) { os << x; return *this; }
} dbg;
void err(istringstream *iss) { delete iss; }
template<class C, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args ...);
}
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args), dbg << endl
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int m, K, T, Q, cn;//}
const int inf = 1000000007, mxn = 21;

vector<int> borrow[mxn];
vector<pair<int, int> > os[mxn];
deque<int> a;
ll n, ans[7];
pair<int, int> o[7][mxn];
int nxt[7][mxn];

bitset<mxn> vstd[7];
bool memo[7][mxn];
bool dp(int b, int at) {
    if (!at) return !b;
    bool &ret = memo[b][at];
    if (vstd[b][at]) return ret;
    vstd[b][at] = 1;
    int x = a[at]-b+10*(a[at]<b);
    ret = 0;
    auto it = os[x].begin();
    for (auto &i : borrow[x]) {
        if (dp((a[at]<b)+i, at-1)) { ret = 1, o[b][at] = *it, nxt[b][at] = (a[at]<b)+i; break; }
        it++;
    }
    return ret;
}

void resetAll() {
    memset(vstd, 0, sizeof(vstd)), a.clear();
}

signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.precision(11);
/*
From right to left, we will try to keep some 0, 4 and 7. Some needs to be propagated (max 4+1, as 6*7 = 42). ==>
Number of states = 20*5.
*/
    for (int i = 0; i <= 6; i++) {
        for (int j = 0; i+j <= 6; j++) {
            int x = i*4+j*7;//bug(i, j, x);
            borrow[x%10].push_back(x/10), os[x%10].push_back({i, j});
        }
    }
    cin >> T;
    while (cn++!=T) {
        resetAll();
        cin >> n;
        while (n) {
            a.push_front(int(n%10)), n /= 10;
        }
        a.push_front(0);
        auto x = dp(0, sz(a)-1);
        if (!x) { cout << "-1\n"; continue; }
        memset(ans, 0, sizeof(ans));
        int b = 0;
        ll p10 = 1;
        for (int i = sz(a)-1; i >= 1; i--) {
            for (int j = 0; j < o[b][i].first; j++) {
                ans[j] += p10*4;
            }
            for (int j = 0; j < o[b][i].second; j++) {
                ans[j+o[b][i].first] += p10*7;
            }
            b = nxt[b][i], p10 *= 10;
        }
        for (int i = 0; i < 6; i++) {
            cout << ans[i] << " \n"[i==5];
        }
    }
}


/*Bismillahir Rahmanir Rahim*///{
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout;   // replace by cerr in interactive.
template<class C> static auto dud(C *x)->decltype(os << *x, 0);
template<class C> static char dud(...);
template<class C> typename enable_if<sizeof dud<C>(0)!=1, Dbg&>::type operator<<(const C &x) { os << x; return *this; }
template<class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i!=e and cur<26; i++, cur++) *this << ", "+2*(i==b) << *i;
    return *this << ']';
}
template<class C> typename enable_if<sizeof dud<C>(0)==1, Dbg&>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
}
template<class C, size_t X> typename enable_if<sizeof(C)!=1, Dbg&>::type operator <<(C (&x)[X]) {
    return dump(begin(x), end(x));
}
template<class B, class C> Dbg &operator <<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
}
template<class C, size_t Y> struct TP {
    void operator()(Dbg& os, const C &t) { TP<C, Y-1>()(os, t); os << ", " << get<Y-1>(t); }
};
template<class C> struct TP<C, 1> { void operator()(Dbg& os, const C &t) { os << get<0>(t); } };
template<class... C> Dbg& operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t); return *this << '}';
}
Dbg &operator<<(ostream&(*x)(std::ostream&)) { os << x; return *this; }
} dbg;
void err(istringstream *iss) { delete iss; }
template<class C, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args ...);
}
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args), dbg << endl
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int m, K, T, Q, cn;//}
const int inf = 1000000007, mxn = 21;

vector<int> borrow[mxn];
vector<pair<int, int> > os[mxn];
deque<int> a;
ll n, ans[7];
pair<int, int> o[7][mxn];
int nxt[7][mxn];

bitset<mxn> vstd[7];
bool memo[7][mxn];
bool dp(int b, int at) {
    if (!at) return !b;
    bool &ret = memo[b][at];
    if (vstd[b][at]) return ret;
    vstd[b][at] = 1;
    int x = a[at]-b+10*(a[at]<b);
    ret = 0;
    auto it = os[x].begin();
    for (auto &i : borrow[x]) {
        if (dp((a[at]<b)+i, at-1)) { ret = 1, o[b][at] = *it, nxt[b][at] = (a[at]<b)+i; break; }
        it++;
    }
    return ret;
}

void resetAll() {
    memset(vstd, 0, sizeof(vstd)), a.clear();
}

signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.precision(11);
/*
From right to left, we will try to keep some 0, 4 and 7. Some needs to be propagated (max 4+1, as 6*7 = 42). ==>
Number of states = 20*5.
*/
    for (int i = 0; i <= 6; i++) {
        for (int j = 0; i+j <= 6; j++) {
            int x = i*4+j*7;//bug(i, j, x);
            borrow[x%10].push_back(x/10), os[x%10].push_back({i, j});
        }
    }
    cin >> T;
    while (cn++!=T) {
        resetAll();
        cin >> n;
        while (n) {
            a.push_front(int(n%10)), n /= 10;
        }
        a.push_front(0);
        auto x = dp(0, sz(a)-1);
        if (!x) { cout << "-1\n"; continue; }
        memset(ans, 0, sizeof(ans));
        int b = 0, p10 = 1;
        for (int i = sz(a)-1; i >= 1; i--) {
            for (int j = 0; j < o[b][i].first; j++) {
                ans[j] += p10*4;
            }
            for (int j = 0; j < o[b][i].second; j++) {
                ans[j+o[b][i].first] += p10*7;
            }
            b = nxt[b][i], p10 *= 10;
        }
        for (int i = 0; i < 6; i++) {
            cout << ans[i] << " \n"[i==5];
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 22;

int t, fin;
long long n;
int f[N][N];
int good[] = {0, 4, 7};
int goal[N];
long long ans[N];

int call (int at, int carry) {
  if (at == fin) {
    return carry == 0;
  }
  if (f[at][carry] != -1) {
    return f[at][carry];
  }
  f[at][carry] = 0;
  for (int i1 : good) {
    for (int i2 : good) {
      for (int i3 : good) {
        for (int i4 : good) {
          for (int i5 : good) {
            for (int i6 : good) {
              int now = i1 + i2 + i3 + i4 + i5 + i6 + carry;
              if (now % 10 != goal[at]) continue;
              f[at][carry] |= call(at + 1, now / 10);
              if (f[at][carry]) {
                ans[0] *= 10, ans[0] += i1;
                ans[1] *= 10, ans[1] += i2;
                ans[2] *= 10, ans[2] += i3;
                ans[3] *= 10, ans[3] += i4;
                ans[4] *= 10, ans[4] += i5;
                ans[5] *= 10, ans[5] += i6;
                return 1;
              }
            }
          }
        }
      }
    }
  }
  return 0;
}

int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld", &n);
    fin = 0;
    while (n) {
      goal[fin++] = n % 10;
      n /= 10;
    }
    memset(f, -1, sizeof f);
    memset(ans, 0, sizeof ans);
    if (call(0, 0)) {
      for (int i = 0; i < 6; ++i) {
        printf("%lld ", ans[i]);
      }
      puts("");
    } else {
      puts("-1");
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = (int)5005;

int num[3] = {0, 4, 7};
string s;
map<int, vector<int>> mp;
bool dp[20][10];
int las[20][10], sav[20][10];

void solve() {
    memset(dp, false, sizeof(dp));
    s = "0" + s;
    int n = s.length();
    dp[n-1][0] = true;
    for(int i = n-1; i >= 1; i--) {
        for(const auto &p : mp) {
            int b = ((s[i] - '0') - p.first % 10 + 10) % 10;
            if(dp[i][b]) {
                int a = (p.first + b) / 10;
                dp[i-1][a] = true;
                las[i-1][a] = b;
                sav[i-1][a] = p.first;
            }
        }
    }
    if(!dp[0][s[0]-'0']) cout << "-1" << endl;
    else {
        vector<ll> res(6, 0);
        int cur = s[0] - '0';
        for(int i = 0; i < n - 1; i++) {
            int x = sav[i][cur];
            for(int j = 0; j < 6; j++) {
                res[j] *= 10;
                res[j] += mp[x][j];
            }
            cur = las[i][cur];
        }
        for(int j = 0; j < 6; j++) cout << res[j] << " ";
        cout << endl;
    }
}

int main() {
    for (int mask = 0; mask < 729; ++mask) {
        int j = mask;
        vector<int> V;
        int sum = 0;
        for(int i = 0; i < 6; i++) {
            V.push_back(num[j % 3]);
            sum += num[j % 3];
            j /= 3;
        }
        mp[sum] = V;
    }
    int T;
    cin >> T;
    while(T--) {
        cin >> s;
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = (int)5005;

int num[3] = {0, 4, 7};
string s;
map<int, vector<int>> mp;
bool dp[20][10];
int las[20][10], sav[20][10];

void solve() {
    memset(dp, false, sizeof(dp));
    s = "0" + s;
    int n = s.length();
    dp[n-1][0] = true;
    for(int i = n-1; i >= 1; i--) {
        for(const auto &p : mp) {
            int b = ((s[i] - '0') - p.first % 10 + 10) % 10;
            if(dp[i][b]) {
                int a = (p.first + b) / 10;
                dp[i-1][a] = true;
                las[i-1][a] = b;
                sav[i-1][a] = p.first;
            }
        }
    }
    if(!dp[0][s[0]-'0']) cout << "-1" << endl;
    else {
        vector<int> res(6, 0);
        int cur = s[0] - '0';
        for(int i = 0; i < n - 1; i++) {
            int x = sav[i][cur];
            for(int j = 0; j < 6; j++) {
                res[j] *= 10;
                res[j] += mp[x][j];
            }
            cur = las[i][cur];
        }
        for(int j = 0; j < 6; j++) cout << res[j] << " ";
        cout << endl;
    }
}

int main() {
    for (int mask = 0; mask < 729; ++mask) {
        int j = mask;
        vector<int> V;
        int sum = 0;
        for(int i = 0; i < 6; i++) {
            V.push_back(num[j % 3]);
            sum += num[j % 3];
            j /= 3;
        }
        mp[sum] = V;
    }
    int T;
    cin >> T;
    while(T--) {
        cin >> s;
        solve();
    }
}


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // BEGIN initialize singles array
        int[] temp = new int[3];
        temp[0] = 0;
        temp[1] = 4;
        temp[2] = 7;
        HashMap<Integer, ArrayList<Integer>> singles = new HashMap<Integer, ArrayList<Integer>>();
        for (int i = 0; i < 43; i++) {
            if (!singles.containsKey(i)) {
                for (int j = 0; j < 3; j++) {
                    if (!singles.containsKey(i)) {
                        for (int k = j; k < 3; k++) {
                            if (!singles.containsKey(i)) {
                                for (int l = k; l < 3; l++) {
                                    if (!singles.containsKey(i)) {
                                        for (int m = l; m < 3; m++) {
                                            if (!singles.containsKey(i)) {
                                                for (int n = m; n < 3; n++) {
                                                    if (!singles.containsKey(i)) {
                                                        for (int o = n; o < 3; o++) {
                                                            if (temp[j]
                                                                    + temp[k]
                                                                    + temp[l]
                                                                    + temp[m]
                                                                    + temp[n]
                                                                    + temp[o] == i) {
                                                                if (!singles
                                                                        .containsKey(i)) {
                                                                    ArrayList tempList = new ArrayList<Integer>();
                                                                    tempList.add(temp[j]);
                                                                    tempList.add(temp[k]);
                                                                    tempList.add(temp[l]);
                                                                    tempList.add(temp[m]);
                                                                    tempList.add(temp[n]);
                                                                    tempList.add(temp[o]);
                                                                    singles.put(
                                                                            i,
                                                                            tempList);
                                                                } else {
                                                                    o = 3;
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        n = 3;
                                                    }
                                                }
                                            } else {
                                                m = 3;
                                            }
                                        }
                                    } else {
                                        l = 3;
                                    }
                                }
                            } else {
                                k = 3;
                            }
                        }
                    } else {
                        j = 3;
                    }
                }
            }
        }
        // END initialize singles array
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            char[] nString = in.next().toCharArray();
            int[] n = new int[nString.length];
            for (int j = 0; j < n.length; j++) {
                n[j] = Character.digit(nString[j], 10);
            }
            ArrayList<Integer> able = new ArrayList<Integer>();
            boolean foundIt = false;
            if (n[0] - 8 <= 4 && n[0] - 8 >= 0) {
                able.add(8);
            }
            if (n[0] - 7 <= 4 && n[0] - 7 >= 0) {
                able.add(7);
            }
            if (n[0] - 4 <= 4 && n[0] - 4 >= 0) {
                able.add(4);
            }
            if (n[0] - 0 <= 4 && n[0] - 0 >= 0) {
                able.add(0);
            }
            if(n.length > 1){
            ArrayList<Integer> answer = new ArrayList<Integer>();
            for (int j = 0; j < able.size(); j++) {
                int tens = n[0] - able.get(j);
                if (!foundIt) {
                    foundIt = f(n, tens, singles, 1, answer);
                    if (foundIt) {
                        answer.add(able.get(j));
                    }
                }
            }
            if (!foundIt) {
                System.out.println(-1);
            } else {
                long[] finalAnswer = new long[6];
                for (int j = 0; j < 6; j++) {
                    for (int k = 0; k < n.length; k++) {
                        finalAnswer[j] += singles.get(answer.get(k)).get(j)
                                * (long)Math.pow(10, k);
                    }
                    System.out.print(finalAnswer[j] + " ");
                }
            }
            }
            else{
                if(!singles.containsKey(n[0])){
                    System.out.println(-1);
                }
                else{
                    for(int j = 0; j < 6; j++){
                        System.out.print(singles.get(n[0]).get(j) + " ");
                    }
                }
            }
        }
    }

    public static boolean f(int[] n, int tens,
            HashMap<Integer, ArrayList<Integer>> singles, int current,
            ArrayList<Integer> answer) {
        boolean foundIt = false;

        if (current + 1 == n.length) {
            int tempNumber = tens * 10 + n[current];
            if (singles.containsKey(tempNumber)) {
                answer.add(tempNumber);
                return true;
            }
            return false;
        }

        ArrayList<Integer> able = new ArrayList<Integer>();
        for (int i = 0; i < 6 && i <= n[current]; i++) {
            int tempNumber = tens * 10 + n[current] - i;
            if (singles.containsKey(tempNumber)) {
                able.add(n[current] - i);
            }
        }
        int singlesDigit = 0;
        for (int i = 0; i < able.size(); i++) {
            if (!foundIt) {
                foundIt = f(n, n[current] - able.get(i), singles, current + 1,
                        answer);
                if (foundIt) {
                    singlesDigit = able.get(i);
                }
            }
        }
        // BEGIN take one and add 10
        if (!foundIt) {
            if (tens > 0) {
                tens -= 1;
                int tempDigit = n[current] + 10;
                able.clear();
                // BEGIN find able again
                for (int i = 0; i < 6 && i <= tempDigit; i++) {
                    if (tempDigit - i < 10) {
                        int tempNumber = tens * 10 + tempDigit - i;
                        if (singles.containsKey(tempNumber)) {
                            able.add(tempDigit - i);
                        }
                    }
                }
                // END find able again
                for (int i = 0; i < able.size(); i++) {
                    if (!foundIt) {
                        foundIt = f(n, tempDigit - able.get(i), singles,
                                current + 1, answer);
                        if (foundIt) {
                            singlesDigit = able.get(i);
                        }
                    }
                }
            }
        }
        // END take one and add 10
        if (foundIt) {
            answer.add(tens * 10 + singlesDigit);
        }
        return foundIt;
    }

}


import java.util.Scanner;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {
        // BEGIN initialize singles array
        int[] temp = new int[3];
        temp[0] = 0;
        temp[1] = 4;
        temp[2] = 7;
        HashMap<Integer, ArrayList<Integer>> singles = new HashMap<Integer, ArrayList<Integer>>();

        for (int i = 0; i < 43; i++) {
            if (!singles.containsKey(i)) {
                for (int j = 0; j < 3; j++) {
                    if (!singles.containsKey(i)) {
                        for (int k = j; k < 3; k++) {
                            if (!singles.containsKey(i)) {
                                for (int l = k; l < 3; l++) {
                                    if (!singles.containsKey(i)) {
                                        for (int m = l; m < 3; m++) {
                                            if (!singles.containsKey(i)) {
                                                for (int n = m; n < 3; n++) {
                                                    if (!singles.containsKey(i)) {
                                                        for (int o = n; o < 3; o++) {
                                                            if (temp[j]
                                                                    + temp[k]
                                                                    + temp[l]
                                                                    + temp[m]
                                                                    + temp[n]
                                                                    + temp[o] == i) {
                                                                if (!singles
                                                                        .containsKey(i)) {
                                                                    ArrayList tempList = new ArrayList<Integer>();
                                                                    tempList.add(temp[j]);
                                                                    tempList.add(temp[k]);
                                                                    tempList.add(temp[l]);
                                                                    tempList.add(temp[m]);
                                                                    tempList.add(temp[n]);
                                                                    tempList.add(temp[o]);
                                                                    singles.put(
                                                                            i,
                                                                            tempList);
                                                                } else {
                                                                    o = 3;
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        n = 3;
                                                    }
                                                }
                                            } else {
                                                m = 3;
                                            }
                                        }
                                    } else {
                                        l = 3;
                                    }
                                }
                            } else {
                                k = 3;
                            }
                        }
                    } else {
                        j = 3;
                    }
                }
            }
        }
        // END initialize singles array
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            char[] nString = in.next().toCharArray();
            int[] n = new int[nString.length];
            for (int j = 0; j < n.length; j++) {
                n[j] = Character.digit(nString[j], 10);
            }
            ArrayList<Integer> able = new ArrayList<Integer>();
            boolean foundIt = false;
            if (n[0] - 8 <= 4 && n[0] - 8 >= 0) {
                able.add(8);
            }
            if (n[0] - 7 <= 4 && n[0] - 7 >= 0) {
                able.add(7);
            }
            if (n[0] - 4 <= 4 && n[0] - 4 >= 0) {
                able.add(4);
            }
            if (n[0] - 0 <= 4 && n[0] - 0 >= 0) {
                able.add(0);
            }
            if(n.length > 1){
            ArrayList<Integer> answer = new ArrayList<Integer>();
            for (int j = 0; j < able.size(); j++) {
                int tens = n[0] - able.get(j);
                if (!foundIt) {
                    foundIt = f(n, tens, singles, 1, answer);
                    if (foundIt) {
                        answer.add(able.get(j));
                    }
                }
            }
            if (!foundIt) {
                System.out.println(-1);
            } else {
                long[] finalAnswer = new long[6];
                for (int j = 0; j < 6; j++) {
                    for (int k = 0; k < n.length; k++) {
                        finalAnswer[j] += singles.get(answer.get(k)).get(j)
                                * (long)Math.pow(10, k);
                    }
                    System.out.print(finalAnswer[j] + " ");
                }
            }
            }
            else{
                if(!singles.containsKey(n[0])){
                    System.out.println(-1);
                }
                else{
                    for(int j = 0; j < 6; j++){
                        System.out.print(singles.get(n[0]).get(j) + " ");
                    }
                }
            }
        }
    }

    public static boolean f(int[] n, int tens,
            HashMap<Integer, ArrayList<Integer>> singles, int current,
            ArrayList<Integer> answer) {
        boolean foundIt = false;

        if (current + 1 == n.length) {
            int tempNumber = tens * 10 + n[current];
            if (singles.containsKey(tempNumber)) {
                answer.add(tempNumber);
                return true;
            }
            return false;
        }

        ArrayList<Integer> able = new ArrayList<Integer>();
        for (int i = 0; i < 6 && i <= n[current]; i++) {
            int tempNumber = tens * 10 + n[current] - i;
            if (singles.containsKey(tempNumber)) {
                able.add(n[current] - i);
            }
        }
        int singlesDigit = 0;
        for (int i = 0; i < able.size(); i++) {
            if (!foundIt) {
                foundIt = f(n, n[current] - able.get(i), singles, current + 1,
                        answer);
                if (foundIt) {
                    singlesDigit = able.get(i);
                }
            }
        }
        // BEGIN take one and add 10
        if (!foundIt) {
            if (tens > 0) {
                tens -= 1;
                int tempDigit = n[current] + 10;
                able.clear();
                // BEGIN find able again
                for (int i = 0; i < 6 && i <= tempDigit; i++) {
                    if (tempDigit - i < 10) {
                        int tempNumber = tens * 10 + tempDigit - i;
                        if (singles.containsKey(tempNumber)) {
                            able.add(tempDigit - i);
                        }
                    }
                }
                // END find able again
                for (int i = 0; i < able.size(); i++) {
                    if (!foundIt) {
                        foundIt = f(n, tempDigit - able.get(i), singles,
                                current + 1, answer);
                        if (foundIt) {
                            singlesDigit = able.get(i);
                        }
                    }
                }
            }
        }
        // END take one and add 10
        if (foundIt) {
            answer.add(tens * 10 + singlesDigit);
        }
        return foundIt;
    }

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * TODO Put here a description of what this class does.
 *
 * @author songm. Created Oct 16, 2013.
 */
public class Main {

    /**
     * TODO Put here a description of what this method does.
     *
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub.
        // Scanner scanner = new Scanner(System.in);
        // String num = scanner.nextLine();
        // int total = Integer.parseInt(num);
        ArrayList<Integer> array = new ArrayList<Integer>();
        ArrayList<Integer> sum = new ArrayList<Integer>();
        ArrayList<String> what = new ArrayList<String>();

        array.add(40);
        array.add(47);
        array.add(74);
        array.add(77);
        array.add(70);
        array.add(400);

        Collections.sort(array);
        System.out.println(array);
        int sum2 = 0;
        int sum3 = 0;
        for(int m = 0; m< array.size(); m++) {
            for (int i = 0; i < 8; i++) {

                for (int j = i + 1; j < 8; j++) {
                    what.add(sum2+"+"+sum3+"="+(sum2+sum3));
                    sum.add(sum2 + sum3);
                    sum2 += 7;
                }
                sum3 += 4;
                sum2 = 0;
            }
            sum3 = array.get(m);
        }

        Collections.sort(sum);
        Collections.sort(what);
        System.out.println(sum);
        System.out.println(what);
        // for (int i = 0; i < total; i++) {
        // String next = scanner.nextLine();
        // int nextInt = Integer.parseInt(num);
        // array.add((long) 0);
        // for (int j = 0; j < 17; j++) {
        // array.add((long) (4 * Math.pow(10, j)));
        // array.add((long) (7 * Math.pow(10, j)));
        // }
        //
        //
        //
        // for(int m = 0; m< array.size(); m++){
        // for(int n = 0; n<array.size(); )
        // }
        // }

    }

}