#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>

using namespace std;

string i_s(int n){
  stringstream s;
  string str;

  s << n;
  s >> str;

  return str;
}

int s_i(string str){
  stringstream s(str);
  int num;

  s >> num;

  return num;
}

int main(){
  string str;
  int n;

  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cout << "Case " << i+1 << ":" << endl;
    cin >> str;
    int num = s_i(str);
    for(int j = 0 ; j < 10 ; j++){
      num = num * num;
      string s = i_s(num);
      reverse(s.begin(),s.end());
      for(int k = (int)s.size() ; k < 8 ; k++){
	s += "0";
      }      
      reverse(s.begin(),s.end());
      string ss = s.substr(2,4);
      num = s_i(ss);
      cout << num << endl;
    }
  }

  return 0;
}