#include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int dt,hoge,n;
	cin>>n;
	rep(p,n){
		cin>>dt;
		cout<<"Case "<<p+1<<":"<<endl;
		rep(q,10){
			hoge=dt*dt;
			hoge-=(int)(hoge/1e6)*1e6;
			hoge/=100;
			cout<<hoge<<endl;
			dt=hoge;
		}
	}
	return 0;
}