#include<iostream>
#include<cstdio>
using namespace std;

int makerand(int r);

int main(){
    int n;
    cin >>n;
    for(int i = 0; i < n; i++){
        int r;
        cin >>r;
        cout <<"Case " <<i + 1 <<":" <<endl;
        for(int i = 0; i < 10; i++){
            int nextr = makerand(r);
            cout <<nextr <<endl;
            r = nextr;
        }
    }
    return 0;
}

int makerand(int r){
    int r2 = r * r;
    int ret = 0, f = 100, tmpf = 1;
    for(int i = 0; i < 4; i++){
        ret += (r2 / f % 10 * tmpf);
        f *= 10;
        tmpf *= 10;
    }
    return ret;
}

//0, 00:28:01