#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>
using namespace std;
int main(){
long n,m,i,j;
string s;
stringstream ss;
string::size_type k;
char mm[9];
cin>>n;
for(i=0;i<n;i++){
cin>>m;
cout<<"Case "<<i+1<<":"<<endl;
for(j=0;j<10;j++){
sprintf(mm,"%08d",m*m);
s=string(mm);
s=s.substr(2,4);
k=s.find_first_not_of('0');
cout<<s.substr(k)<<endl;
ss.str("");ss.clear();
ss<<s;
ss>>m;
}
}
return 0;
}