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

int n = 4;

string int2str(int a){
    stringstream ss;
    ss << a;
    return ss.str();
}

int main(void){
  int s;
  int cnt = 1;
  int num;
  cin >> num;
  while(num--){
    cin >> s;
    cout << "Case " << cnt  << ":" << endl;
    cnt++;
    for(int q=0; q < 10; q++){
      int u = s * s;
      string t(int2str(u));
      t.insert(0, 2*n - t.length(), '0');
      t = t.substr(n/2, n);
      s = atoi(t.c_str());
      cout << s << endl;
    }
  }
  return 0;
}