#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  cin >> n;
  REP(casen,n) {
    int a;
    cin >> a;
    cout << "Case " << casen+1 << ":" << endl;
    REP(k,10) {
      a*=a;
      stringstream ss;
      ss << a;
      string s = ss.str();
      int le = s.length();
      REP(i,8-le) {
        s = "0" + s;
      }
      a = atoi(s.substr(2,4).c_str());
      cout << a << endl;
    }
  }
}