#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,a;

int d_count(int x){
  int res=0;
  while(x>0){
    res++;
    x/=10;
  }
  return res;
}

vector<int> compute(int x){
  vector<int>res;
  x=x*x;
  int cnt = d_count(x);
  while(x>0){
    res.push_back(x%10);
    x/=10;
  }
  for(int i=0;i<(8-cnt);i++)res.push_back(0);
  reverse(res.begin(),res.end());
  return res;
}

int main(){
  vector<int>t;
  cin>>n;
  for(int I=1;I<=n;I++){
    t.clear();
    cin>>a;
    cout<<"Case "<<I<<':'<<endl;
    for(int i=0;i<10;i++){
      t=compute(a);

      //for(int i=0;i<t.size();i++)cout<<t[i]<<' ';cout<<endl;
      a=t[2]*1000+t[3]*100+t[4]*10+t[5];
      cout<<a<<endl;
    }
  }
  return 0;
}