#include <iostream>
#include <vector>

using namespace std;

vector<int> temp;

void Compute(int num);

int main() {

	int n;
	cin >> n;
	int j = 1;

	while (n--) {

		int num;
		cin >> num;
		cout << "Case " << j << ":\n";

		for (int i = 0; i < 10; i++) {

			temp.clear();
			Compute(num);
			num = temp[5] * 1000 + temp[4] * 100 + temp[3] * 10 + temp[2];
			cout << num << "\n";

		}

		j++;

	}

	return 0;

}

void Compute(int num) {

	num *= num;
	while (num) {

		temp.push_back(num % 10);
		num /= 10;

	}

	int times = temp.size();
	for (int i = 0; i < 8 - times; i++)
		temp.push_back(0);
}

