#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
	int n, x, i, j;
	char c[9], d[5];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> x;
		cout << "Case " << i+1 << ":" << endl;
		for (j = 0; j < 10; j++)
		{
			x *= x;
			sprintf(c, "%08d", x);
			d[0] = c[2], d[1] = c[3], d[2] = c[4], d[3] = c[5], d[4] = '\0';
			x = atoi(d);
			cout << x << endl;
		}
	}
}