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

int n = 4;

void solve(){
	int s; cin >> s;
	for (int i = 0; i < 10; i++){
		string str = to_string(s*s);
		if (str.size() < 8){
			string str2;
			for (int j = 0; j < 8 - str.size(); j++) str2+='0';
			str2 += str;
			str = str2;
		}
		string ret = "0000";
		for (int j = 0; j < 4; j++){
			ret[j] = str[j + 2];
		}
		int ans = stoi(ret);
		cout << ans << endl;
		s = ans;
	}
}

int main(){
	int num; cin >> num;
	for (int i = 0; i < num; i++){
		cout << "Case " << i + 1 << ":" << endl;
		solve();
	}
	return 0;
}