#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int calc(int x) {
  char buf[128];
  sprintf(buf, "%08d", x*x);
  buf[6] = '\0';
  return atoi(buf+2);
}

int main() {
  int n;
  cin >> n;
  for(int tc = 0; tc < n; ++tc) {
    int x;
    cin >> x;
    cout << "Case " << tc+1 << ":" << endl;
    for(int i = 0; i < 10; ++i) {
      cout << (x = calc(x)) << endl;
    }
  }
  return 0;
}