# include<bits/stdc++.h>
using namespace std;
const int maxn=1e7;
bool v[maxn];
bool flag=true;
bool qz[maxn];
bool hz[maxn];
int prepare[6]={1,10,100,1000,10000};
int cnt=0,sum,start;
int L[10],R[10],dj1,dj2;
bool check(int x)
{
if (x/10000+((x%10000)/1000)+((x%1000)/100)+((x%100)/10)+(x%10)==sum)
return true;
else
return false;
}
void isprime()
{
v[1]=true;
int n=99999;
for (int i=2;i<=n;i++)
{
if (!v[i])
{
if (i>=10000&&i<=99999&&check(i))
{
qz[i/10000]=qz[i/1000]=qz[i/100]=qz[i/10]=qz[i]=true;
hz[i%10000]=hz[i%1000]=hz[i%100]=hz[i%10]=hz[i]=true;
}
for (int j=1;j<=n/i;j++)
{
v[i*j]=true;
}
}
}
}
void output()
{
for (int i=1;i<=5;i++)
{
printf("%d\n",L[i]);
}
printf("\n");
}
void search(int x,int y)
{
if (y==6)
{
x=x+1;
y=1;
}
if (x==6)
{
flag=false;
output();
return ;
}
bool c1,c2,c3,c4;
for (int i=0;i<=9;++i)
{
c1=qz[L[x]*10+i];
c2=qz[R[y]*10+i];
c3=(x!=y)||(x==y&&qz[dj1*10+i]);
c4=(x+y!=6)||(x+y==6&&hz[dj2+i*prepare[x-1]]);
if (!(c1&&c2&&c3&&c4)) continue;
L[x]=L[x]*10+i;
R[y]=R[y]*10+i;
if (x==y) dj1=dj1*10+i;
if (x+y==6) dj2=dj2+i*prepare[x-1];
search(x,y+1);
L[x]/=10;
R[y]/=10;
if (x==y) dj1/=10;
if (x+y==6) dj2-=i*prepare[x-1];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&sum,&start);
isprime();
L[1]=R[1]=dj1=start;
search(1,2);
if(flag)
{
printf("NONE\n");
printf("\n");
}
memset(qz,false,sizeof(qz));
memset(hz,false,sizeof(hz));
dj1,dj2=0;
}
return 0;
}