#include<bits/stdc++.h>
using namespace std;

void randed(int num,int idx){
  cout << "Case " << idx << ":" << endl;
  for(int i=0;i<10;i++){
    int tmp = num*num;
    tmp /= 100;
    int next = 0;
    for(int j=0;j<4;j++){
      if(tmp == 0) break;
      int s = tmp % 10;
      int k = 1;
      for(int z=0;z<j;z++){
	k *= 10;
      }
      next += s * k;
      tmp /= 10;
    }
    cout << next << endl;
    num = next;
  }
}

int main(){
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    int num;
    cin >> num;
    randed(num,i+1);
  }
}