#include<iostream>
using namespace std;
int main(){
  int n,ans,o,d,k;
  cin >> o;
  for(int i=1;i<=o;i++){
    cin >> n;
    cout << "Case " << i << ":" << endl;
    for(int i=0;i<10;i++){
      ans = 0;
      d = 100000;
      k = 1000;
      n *= n;
      for(int j=0;j<4;j++){
	ans += n/d%10*k;
	d /=10;
	k /= 10;
      }
      n = ans;
      cout << ans << endl;
    }
  }
    return 0;
}