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

int main(){
    char num[9],a[5];
    int n,tmp;
    cin >> n;
    for(int j=0;j<n;j++){
        cin >> tmp;
        cout << "Case " << j+1 << ":" << endl;
        for(int i=0;i<10;i++){
            tmp *= tmp;
            sprintf(num,"%08d",tmp);
            for(int i=0;i<4;i++) a[i] = num[i+2];
            tmp = atoi(a);
            cout << tmp << endl;
        }
    }
}