#include <iostream>
#include <fstream>

using namespace std;

fstream start, wynik;

int a=0, b=0, c=0, krrach;

int rozkladIsuma(int x)
{
    int tym=0, tym2;
    while(x!=0)
    {
        tym2=x%10;
        tym=tym+(tym2*tym2);
        x=x/10;
    }
    return tym;
}

int main()
{
    start.open("wes_dane.txt", ios::in);
    wynik.open("wes_odp.txt", ios::app);
    start>>c;
    for(int ii=1; ii!=c; ii++)
    {
        start>>a;
        b=a;
        bool check=false;
        cout<<a<<endl;
        while(check != true)
        {
            if (a==1||a==4)
            {
                krrach=a;
                check=true;
                cout<<"\n";
            }
            a=rozkladIsuma(a);
            cout << a << "\n";
        }
        wynik<<b<<" | "<<krrach<<endl;
    }

    return 0;
}
