#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	for(int x=1;x<=n;++x){
		cout << "Case "<<x<<":\n";
		int T;
		cin>>T;
		for(int i=0; i<10; ++i){
			T=T*T;
			T%=100000000;
			T=	((T%1000000)/100000)*1000+
				((T%100000)/10000)*100+
				((T%10000)/1000)*10+
				((T%1000)/100);
			cout << T << '\n';
		}
	}
	return 0;
}