#include "stdio.h"

int main(){
	int i,j,x;
	int n,N;
	scanf("%d\n",&N);
	char str[9];
	for(x=1;x<=N;x++){
		scanf("%d\n",&n);
		printf("Case %d:\n",x);
		for(i=0;i<10;i++){
			sprintf(str,"%08d",n*n);
			str[6] = 0;
			n = atoi(str+2);
			printf("%d\n",n);
		}
	}
	return 0;
}
	