#include<iostream>
#include<algorithm>

using namespace std;

int num;

string toc(int num){
  string out;
  while(num > 0){
    out += num%10 + '0';
    num /= 10;
  }
  while(out.length() < 8) out += '0';
  reverse(out.begin(),out.end());
  return out;
}

int toi(string in){
  int out = 0;
  int plus = 1;
  for(int i = in.length()-1; i >= 0; i--){
    out += (in[i] - '0') * plus;
    plus *= 10;
  }
  return out;
}
void solve(){
  for(int i = 0; i < 10; i++){
    string sub;
    num = num*num;
    string str = toc(num);
    int flg = 0;
    for(int j = 2; j < 6; j++){
      sub += str[j];
      if(str[j] != '0') flg = 1;
      if(flg) cout << str[j];
    }
    cout << endl;
    num = toi(sub);
  }
}

int main(){
  int in;
  cin >> in;
  int rev = 0;
  for(int i = 0; i < in; i++){
    rev++;
    cin >> num;
    cout << "Case " << rev <<":" <<endl;
    solve();
  }
  return 0;
}