#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n; cin>>n;
  REP(c,n){
    int s; cin>>s;
    printf("Case %d:\n",c+1);
    REP(i,10){
      char buff[16];
      sprintf(buff,"%08d",s*s);
      buff[6] = '\0';
      sscanf(buff+2,"%d",&s);
      printf("%d\n",s);
    }
  }
  return 0;
}