
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>
#include<iterator>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define rep(i,n) for(int i=0; i<n; i++)
#define INF (1<<20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int main()
{
	int n;
	cin >> n;

	rep(i, n) {
		ll num;
		cin >> num;

		cout << "Case " << i + 1 << ":" << endl;
		rep(j, 10) {
			num = num*num;
			string str = to_string(num);
			while (str.size() < 8)
				str = "0" + str;

			str = str.substr(2,4);
			num = stoll(str);
			cout << num << endl;
		}
	}
	return 0;
}