#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>
#include <queue>

using namespace std;



int main()
{
	int n, s;
	char p[9], ne[5];
	
	cin >> n;
	for (int i=1; i<=n; i++)
	{
		cin >> s;
		cout << "Case " << i << ":" << endl;
		for (int j=0; j<10; j++)
		{
			sprintf(p, "%08d", s*s);
			ne[0]=p[2];ne[1]=p[3];ne[2]=p[4];ne[3]=p[5];ne[4]=0;
			printf("%d\n", atoi(ne));
			s = atoi(ne);
		}
	}
	
	return 0;
}