#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

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

int n;

string i2s(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}

int main() {
  scanf("%d", &n);
  rep (loop,n) {
    printf("Case %d:\n", loop + 1);
    int a;
    scanf("%d", &a);
    rep (i,10) {
      a *= a;
      string temp = i2s(a);
      while (temp.size() != 8) { temp.insert(0, 1, '0'); }
      string next = temp.substr(2, 4);
      a = atoi(next.c_str());
      printf("%d\n", a);
    }
  }
  return 0;
}