#include<iostream>
using namespace std;

int middleSquareMethod( int a )
{
    a *= a;
    a /= 100;

    int sum = 0, d = 1;
    for( int i = 0; i < 4; i++ )
    {
        sum += ( a % 10 ) * d;
        a /= 10;
        d *= 10;
    }

    return sum;
}

int main()
{
    int n, d;

    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> d;
        cout << "Case " << i + 1 << ":" << endl;

        int a = 10, b;
        while(a--)
        {
            b = middleSquareMethod(d);
            cout << b << endl;
            d = b;
        }
    }
    return 0;
}
