#include <iostream>
using namespace std;

int f(int x, int k){
	int n=1;
	for(int i=0 ; i < k ; i++ ) n *= 10;
	return ((x / n) % 10) * (n / 100);
}

int main(){
	int n, s;
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		cin >> s;
		cout << "Case " << (i+1) << ":" << endl;
		for(int j=0 ; j < 10 ; j++ ){
			s = s * s;
			s = f(s, 2) + f(s, 3) + f(s, 4) + f(s, 5);
			cout << s << endl;
		}
	}
}