#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int frand(int n){
	char s[10];
	sprintf(s,"%d",n*n);
	int len=strlen(s);
	
	n=0;
	int idx=len-6;
	while(idx<len-2){
		if(idx<0){
			++idx;
			continue;
		}else{
			n=10*n+(s[idx]-'0');
			++idx;
		}
	}
	
	return n;
}

int main(){
	int n;
	cin>>n;
	int count=0;
	while(n--){
		++count;
		int s;
		cin>>s;
		cout<<"Case "<<count<<":"<<endl;
		for(int i=0;i<10;i++){
			s=frand(s);
			cout<<s<<endl;
		}
	}
}