#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<sstream>


#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)

#define PII pair<int,int>
#define PP pair< int,PII >

#define ci(x) (x-'0')

using namespace std;

int main(){
	
	int n;
	char str[10];
	scanf("%d", &n);
	
	rep(i,n){
		printf("Case %d:\n",i+1);
		int s;
		scanf("%d", &s);
		rep(j,10){
			s = s*s;
			sprintf(str,"%08d", s);
			s = ci(str[2])*1000+ci(str[3])*100+ci(str[4])*10+ci(str[5])*1;
			printf("%d\n",s);
		}
	}
	return 0;
}