#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int d;
  cin >> d;
  for (int i = 0; i < d; i++) {
    cout << "Case " << i + 1 << ":" << endl;
    string s;
    cin >> s;
    for (int j = 0; j < 10; j++) {
      string t = to_string(stoi(s) * stoi(s));
      while (int(t.size()) < 8) t = "0" + t;
      s = t.substr(2, 4);
      cout << stoi(s) << endl;
    }
  }

  return 0;
}
