#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>


using namespace std;

int main(void){
	int n,s;
	scanf("%d",&n);
	for(int a=1;a<=n;a++){
		scanf("%d",&s);
		printf("Case %d:\n",a);
		for(int i=0;i<10;i++){
			char ss[8],sss[4];
			int rank=0;
			s=s*s;
			if(s>=100)rank=1;
			if(s>=1000)rank=2;
			if(s>=10000)rank=3;
			if(s>=100000)rank=4;
			if(s>=1000000)rank=5;
			if(s>=10000000)rank=6;
			sprintf(ss,"%d",s);
			int sk=0;
			for(int j=rank-4;j<rank;j++){
				if(j>=0){
					sss[sk]=ss[j];
					sk++;
				}
			}
			s=atoi(sss);
			printf("%d\n",s);
		}
	}
    return 0;
}