import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;

void main(){

    int n = readln().chomp().to!int;
    for(int j=0;j<n;j++){
	int s = readln().chomp().to!int;
	writef("Case %d:\n",j+1);
	for(int i=0;i<10;i++){
	    string a = to!string(s*s);
	    int size = to!int(a.length);
	    for(int k=0;k<8-size;k++){
		a = '0' ~ a;
	    }
	    if(size==9) a = a[3..$-2];
	    else a = a[2..$-2];
	    int c;
	    if(a == "0000"){
		writeln('0');
		s = 0;
		continue;
	    }
	    while(a[c]=='0'){
	       	c++;
	    }
	    s = to!int(a[c..$]);
	    writeln(s);
	}
    }

}