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

int main(){
	int N;
	cin >> N;
	for(int i=1;i<=N;i++){
		int s;
		cin >> s;
		cout << "Case " << i << ":" << endl;
		for(int j=0;j<10;j++){
			s *= s;
			stringstream ss;
			ss << s;
			string r = ss.str();
			//cout << r << endl;
			//rª3¢ÌêC^ñÌÍ¢ÂàO
			if(r.size()<3){s = 0;continue;}
			//(n>5)?4:n-2;
			//(n>5)?n-6:0;
			int dig = (r.size()>5)?4:r.size()-2, pos = (r.size()>5)?r.size()-6:0;
			ss.str("");
			ss << r.substr(pos,dig);
			ss >> s;
			cout << s << endl;
		}
	}
}