#include<iostream>
#include<algorithm>
#include<string>
#include<list>


using namespace std;


int main()
{
	int n, num;


	ios::sync_with_stdio(false);

	cin >> n;

	for (int i = 0; i != n; ++i)
	{
		cin >> num;
		cout << "Case " << i + 1 << ":" << endl;
		for (int j = 0; j != 10; ++j)
		{
			num = (num*num)%1000000/100;
			cout << num << endl;
		}
	}



	return 0;

}
