#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

int MiddleSquareMethod(int s){
	ostringstream oss;
	oss<<setfill('0')<<setw(8)<<s*s;
	return atoi(oss.str().substr(2,4).c_str());
}

int main() {
	int n,s;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>s;
		cout<<"Case "<<i+1<<":"<<endl;
		for(int j=0;j<10;++j){
			s = MiddleSquareMethod(s);
			cout<<s<<endl;
		}
	}
	return 0;
}