#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;

char buf[50];
int main() {
	ll s;
	int d; cin >> d;
	rep(j, d) {
		cin >> s;
		printf("Case %d:\n", j + 1);
		rep(i, 10) {
			s *= s;
			sprintf(buf, "%08lld", s);
			string ans(buf);
			cout << (s = stoi(ans.substr(2, 4))) << endl;
		}
	}
}