#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;

int Change(int num){
	 num *= num;
	char mid[10]={0};
	sprintf(mid,"%d",num);
	int len = strlen(mid);
	char str[10]={0};
	int z = 0;
	fill(str,str+8,'0');
	reps(i,8-len,8){
		str[i] = mid[z];
		z++;
	}
	char fin[10]={0};
	rep(i,4){
		fin[i] = str[i+2];
	}
	return atoi(fin);
}
int main(){
	int n;
	scanf("%d",&n);
	rep(i,n){
		int num;
		scanf("%d",&num);
		printf("Case %d:\n",i+1);
		rep(j,10){
			num = Change(num);
			printf("%d\n",num);
		}
	}
	return 0;
}