#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
#define N 4
int main(){
    int n,in,tmp;
    cin >> n;
    vector<int> data;
    int t;
    for(int i = 0; i < n; ++i) {
        cout << "Case " << i+1 << ":" << endl;
        cin >> in;
        for(int k = 0; k < 10; ++k) {
            data.clear();
            tmp = pow(in,2);
            for(int j = 0; j < 2*N; ++j) {
                t = pow(10,2*N-j-1);
                if (2*N-N/2 >= j && j >= N/2) {
                    data.push_back(tmp/t);
                }
                tmp = tmp%t;
            }
            int next(0),l(4);
            for(vector<int>::iterator it = data.begin(); it != data.end(); ++it) {
                next += *it * pow(10,--l);
            }
            cout << next << endl;
            in = next;
        }
    }
}