#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
	int n; std::cin >> n;
	for (int i=0; i<n; i++) {
		int s; std::cin >> s;
		std::cout << "Case " << i+1 << ":" << std::endl;
		for (int j=0; j<10; j++) {
			s *= s;
			std::ostringstream oss;
			oss << std::setw(8) << std::setfill('0') << s;
			s = std::stoi(oss.str().substr(2,4));
			std::cout << s << std::endl;
		}
	}
	
	return 0;
}


