#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cout << "Case " << i << ":" << endl;
        int s;
        cin >> s;
        for(int j = 0; j < 10; j++) {
            s = s * s;
            string t(8, '0');
            for(int k = 7; s > 0; k--) {
                t[k] = '0' + (s % 10);
                s /= 10;
            }


            for(int k = 2; k < 6; k++) 
                s = s * 10 + (t[k] - '0');
            
            cout << s << endl;

        }
    }
}