#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
int getDigit( long long n )
{
	int cnt = 0;
	while ( n >= 10 )
	{
		n /= 10;
		cnt++;
	}
	return cnt+1;
}
int main( void )
{
	int n;
	cin >> n;

	for ( int lpc = 0; lpc < n; lpc++ )
	{
		long long s;
		cin >> s;

		cout << "Case " << lpc+1 << ":" << endl;
		for ( int i = 0; i < 10; i++ )
		{
			int d = getDigit( s );
			s *= s;
			ostringstream os;
			os << setfill('0') << setw(8) << s << endl;
			string s_str = os.str();
			string s_next;
			s_next = s_str.substr( 2, 4 );
			istringstream is( s_next );
			is >> s;
			cout << s << endl;
		}

	}

	return 0;
}