#include <bits/stdc++.h>

using namespace std;

template <typename T> string toString(const T& t) {
    ostringstream os; os << t; return os.str();
}

void solve(int s) {
    string w;
    for (int i = 0; i < 10; i++) {
        w = toString(s * s);
        while (w.size() != 8) w = "0" + w;
        w = w.substr(2, 4);
        s = atoi(w.c_str());
        cout << s << endl;
    }
}

int main(void) {
    int d, s;
    cin >> d;
    for (int i = 0; i < d; i++) {
        cin >> s;
        cout << "Case " << i + 1 << ":\n";
        solve(s);
    }

    return 0;
}
