#include "bits/stdc++.h"
using namespace std;

int main(){
	unsigned long long num, buf;
	int a[8], n, ans;

	cin>>n;
	for(int i=1; i<=n; i++){
		cin>>num;
		cout<<"Case "<<i<<":\n";

		for(int j=0; j<10; j++){
			ans=0;
			num*=num;
			buf=num;
			//cout<<"("<<num<<")";
			for(int k=0; k<8; k++){
				a[k] = buf%10;
				buf/=10;
				//cout<<a[k]<<"\n";
			}
			int x=1;
			for(int l=2; l<6; l++){
				ans+=a[l]*x;
				x*=10;
			}
			cout<<ans<<"\n";
			num=ans;
		}
	}
	return 0;
}