//水题一枚,不过让我W的蛋疼。。。
//while(scanf("%d%d",&n,&m)&&n&&m)
//尼玛这样竟然不给A
//while(scanf("%d%d",&n,&m),n||m)换成这样就A了,
//本来一次提交就能过的题,纠结了半天,,,不爽,,,,
#include <stdio.h>
int main ()
{
int c,n,m,a,b;
int count=0;
int cases=1;
scanf("%d",&c);
while(c--)
{
cases=1;
while(scanf("%d%d",&n,&m),n||m)
{
count=0;
for(a=1;a<n;a++)
{
for(b=a+1;b<n;b++)
if((a*a+b*b+m)%(a*b)==0)
count++;
}
printf("Case %d: %d\n",cases,count);
cases++;
}
if(c!=0)printf("\n");
}
return 0;
}