#include<string>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main(){
  int n,a;
  char str[10];

  scanf("%d", &n);
  for(int i=0; i<n; i++){
    cin >> a;
    cout << "Case " << i+1 << ":" << endl;
    for(int j=0; j<10; j++){
    sprintf(str,"%08d",a*a);
    string str2 = string(str);
    string str3 = str2.substr(2,4);
    a = atoi(str3.c_str());
    cout << a << endl;
    }
  }
  return 0;
}