#include <iostream>
using namespace std;

int main() {

  int q;
  cin >> q;

  for(int i=0;i<q;i++) {
    
    int b;
    cin >> b;
    cout <<"Case "<<i+1<<":"<<endl;
    for(int j=0;j<10;j++) {
      int a[8] = {};  
      b *= b;
      for(int k=7;k>=0;k--) {
	a[k] = b % 10;
	b /= 10;
      }
      int t = 1;
      b = 0;
      for(int k=5;k>=2;k--) {
	b += a[k]*t;
	t*=10;
      }
      cout << b << endl;
    }


  }

  return 0;
}