#include <iostream>
using namespace std;

int main(){
  int i,N;
  char suji[9];
  int n; cin >> N;
  int cnt = 1;
  while( N > 0 ){
    N--;
    cin >> n;
    int k = 10;
    cout << "Case" << " " << cnt << ":" << endl;
    cnt++;
    while( k > 0 ){
      k--;
      int s = n*n;
      for( i = 1; i <= 8; i++ ) suji[i] = '0'; 

      int amari_zan = 10,wari_zan = 1,tmp;
      for( i = 8; i >= 1; i-- ){
	tmp = s % amari_zan;
	tmp /= wari_zan;
	suji[i] = tmp + '0';
	amari_zan *= 10;
	wari_zan *= 10;
      }
      int kakeru = 1000;
      n = 0;
      for( i = 3; i <= 6; i++ ){
	n += ( suji[i] - '0' )  * kakeru;
	kakeru /= 10;
      }

      cout << n << endl;
    }
  }
  return 0;
}