#include<math.h>
#include<stdio.h>
using namespace std;
int main() {
string w;
int quit[999],lo,al[999];
//getline(cin,w);
cin>>lo;
cin.ignore(256, '\n');
for(int y=0;y<lo;y++)
{
int u=0,x=0,qw;
float r;
char f[1000][1000];
string str,we="";
getline(cin, str);
//cout<<str;
for(long long int i=0;i<str.length();i++)
{
int b=str;
if(b>=97 && b<=122)
we=we+str;
}
//cout<<we<<endl;
r=sqrt(we.length());
qw=(int)r;
//cout<<qw;
if(qw==r)
{
for(long long int ty=0;ty<qw;ty++)
{
for(long long int qe=0;qe<qw;qe++)
{
f[ty][qe]=we[x];
++x;
}
}
for(long long int hy=0;hy<qw;hy++)
{
for(long long int rq=0;rq<qw;rq++)
{
//cout<<f[hy][rq];
if(f[hy][rq]!=f[rq][hy])
++u;
}
//cout<<endl;
}
}
else u=1;
//cout<<u;
quit[y]=u;
al[y]=qw;
}
for(int p=0;p<lo;p++)
{
if(quit[p]==0)
{
cout<<"Case #"<<p+1<<":"<<endl;
if(p!=(lo-1))
cout<<al[p]<<endl;
else cout<<al[p];
}
else
{cout<<"Case #"<<p+1<<":"<<endl<<"No magic
![:(](./images/smilies/icon_frown.gif)
if(p!=(lo-1))
cout<<endl;
}
}
}