#include <cstdio>

int d,s;

int main(){
	scanf("%d",&d);
	for(int i = 1; i <= d; i++){
		scanf("%d",&s);
		printf("Case %d:\n",i);
		for(int j = 0; j < 10; j++){
			int nxt = s*s;
			nxt %= 1000000;
			nxt /= 100;
			printf("%d\n",nxt);
			s = nxt;
		}
	}
	return 0;
}