#include <bits/stdc++.h>
using namespace std;

void Change(char *conv, char *res)
{
  for (int i = 0; i < 4; i++){
    res[i] = conv[i + 2];
  }
}
int main()
{
  int n, s;

  scanf("%d", &n);

  for (int i = 0; i < n; i++){
    scanf("%d", &s);
    printf("Case %d:\n", i + 1);
    for (int j = 0; j < 10; j++){
      char conv[9], res[5];
      sprintf(conv, "%08d", s * s);
      Change(conv, res);
      s = atoi(res);
      printf("%d\n", s);
    }
  }

  return (0);
}