#include <iostream>
#include <queue>

using namespace std;

int main()
{
  int n;
  int s;
  int x = 1;
  queue<int> qu;

  cin >> n;

  while (n--){
    cout << "Case" << x++ << ":" << endl;
    cin >> s;
    for (int i = 0; i < 10; i++){
      s = s * s;
      int t = 10000000;
      for (int cnt = 0; cnt < 8; cnt++){
        qu.push(s / t);
        s %= t;
        t /= 10;
      }
      s = 0;
      t = 1000;
      for (int cnt = 0; cnt < 8; cnt++){
        if (cnt >= 2 && cnt <= 5){
          s += qu.front() * t;
          t /= 10;
        }
        qu.pop();
      }
      cout << s << endl;
    }
  }

  return (0);
}