#include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
string i2s(int num){
  stringstream ss;
  string str;
  ss << num;
  ss >> str;
  return str;
}
int main(){
  int n;
  cin >> n;
  for(int k = 1 ; k <= n ; k++ ){
    int num;
    cin >> num;
    cout << "Case " << k << ":" << endl;
    for(int i = 0 ; i < 10 ; i++ ){
      string tmp = i2s(pow(num,2));
      string str="";
      for(int i = 0 ; i < 8 - tmp.size() ; i++){
	str+='0';
      }
      str+=tmp;
      str = str.substr(2,4);

      cout << atoi(str.c_str()) << endl;

      num = atoi(str.c_str()); 
    }
  }
  return 0;
}