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

int n, s;

int main()
{
	std::cin >> n;
	for( int i = 0; i != n; ++i )
	{
		std::cin >> s;

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

	return 0;
}