#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int solve(int n) {
    int p = int(pow(n, 2));
    p /= 100;
    int res = 0, t = 1;
    for (int i = 0; i < 4; i++) {
        res += t * (p % 10);
        p /= 10;
        t*=10;
    }
    return res;
}

int main()
{
    int T, n;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++) {
        scanf("%d", &n);
        printf("Case %d:\n", t);
        for (int i = 0; i < 10; i++) {
            n = solve(n);
            printf("%d\n", n);
        }
    }
}
