#include<iostream>
#include<string>
using namespace std;

int main() {
	int n;
	cin >> n;
	for (int x = 1; x <= n; x++) {
		int a;
		cin >> a;
		cout << "Case " << x << ":\n";
		for (int i = 0; i < 10; i++) {
			a *= a;
			int res = 0;
			a -= (a / 10000000) * 10000000;
			a -= (a / 1000000) * 1000000;
			res += (a / 100000) * 1000;
			a -= (a / 100000) * 100000;
			res += (a / 10000) * 100;
			a -= (a / 10000) * 10000;
			res += (a / 1000) * 10;
			a -= (a / 1000) * 1000;
			res += a / 100;
			cout << res << endl;
			a = res;
		}
	}
	return 0;
}