#include <iostream>
#include <sstream>
using namespace std;

int main(void){
  int n,s;
  cin>>n;
  for(int x=0;x<n;++x){
    cin>>s;
    cout<<"Case "<<x+1<<":"<<endl;
    for(int i=0;i<10;++i){
      stringstream ss;
      ss<<s*s;
      string a,b;
      ss>>a;
      for(;a.size()<2*4;)a="0"+a;
      b=a.substr(a.size()/2-4/2,4);
      ss.clear();
      ss<<b;
      ss>>s;
      cout<<s<<endl;
    }
  }
  return 0;
}