#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

string itos_withzero (int n)
{
	stringstream ss;
	ss << n;
	string str;
	ss >> str;

	int len = str.length();

	if (len < 8 ){
		str = string(8-len, '0') + str;
	} // end if

	return str;
} 
int stoi (string str )
{
	stringstream ss(str);
	int n;
	ss >> n;

	return n;
}

int middlesquare (int s )
{
	int res = 0;
	int m = s*s;

	string str = itos_withzero(m);

	str = str.substr (2, 4);
//	cout << str << endl;
	res = stoi (str);
//	cout << itos (m) << endl;

	return res;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.ms", "r", stdin );
	string str = "";
	int n;

	getline (cin, str );
	stringstream ssn(str);
	ssn >> n;

	int i,j;
	for (i = 0; i < n; ++i){
		int s = 0;
		int res = 0;
		cout << "Case " << (i + 1) << ':' << endl;
		getline (cin, str );
		stringstream ss(str);
		ss >> s;

		for (j = 0; j < 10; ++j){
			res = middlesquare (s);
			cout << res << endl;
			s = res;
		} // end for
	} // end loop
		
	return 0;
}