#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
using namespace std;

string itos(int x){
  stringstream ss;
  ss << x;
  return ss.str();
}

int main(){
  int T;
  cin >> T;

  for(int SET = 1; SET <= T; SET++){
    printf("Case %d:\n", SET);

    int n;
    cin >> n;

    for(int i = 0; i < 10; i++){
      n *= n;
      string s = itos(n);

      while(s.length() != 8){
        s = "0" + s;
      }

      string ts = "";

      for(int i = 2; i <= 5; i++){
        ts += s[i];
      }

      n = atoi(ts.c_str());
      cout << n << endl;
    }
  }
}