#include <iostream>
using namespace std;

int main()
{
	int n;

	while( cin >> n ){
		for( int i = 0; i < n; ++i ){
			int s;
			cin >> s;
			cout << "Case " << i + 1 << ":" << endl;
			for( int j = 0; j < 10; ++j ){
				s *= s;
				int d[ 4 ];
				s /= 100;
				for( int k = 0; k < 4; ++k ){
					d[ k ] = s % 10;
					s /= 10;
				}
				s = 0;
				for( int k = 0; k < 4; ++k ){
					s *= 10;
					s += d[ 4 - 1 - k ];
				}
				cout << s << endl;
			}
		}
	}

	return 0;
}