#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){
  int d;
  cin>>d;
  for(int x=0;x<d;x++){
    cout<<"Case "<<x+1<<":"<<endl;
    int n;
    cin>>n;
    for(int i=0;i<10;i++){
      string s=to_string(n*n);
      while(s.size()<8) s='0'+s;
      n=stol(s.substr(2,4));
      cout<<n<<endl;
    }
  }
  return 0;
}