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

int main(){
	int N;
	long long int s;
	cin >> N;
	for(int i = 1;i <= N;i++){
		
		cin >> s;
		cout << "Case " << i << ":" << endl;

		for(int j = 10;j;j--){
			s *= s;
			stringstream ss;
			ss << s;
			string a = ss.str();
			int asize = a.size();
			//a.size()ÍÂÏ
			for(int k = 0;k < 8 - asize;k++){a = "0" + a;}

			ss.str("");
			ss << a.substr(2,4);
			long long int b;
			ss >> b;
			s = b;
			cout << b << endl;
		}
	}
	return 0;
}