OI I - kwa

// https://szkopul.edu.pl/problemset/problem/VwKJTv4dd23eLGYIeUCOZRcj/site/?key=statement
// Suma kwadratów cyfr

#include <algorithm>
#include <iostream>
#include <string>

// halu

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int t;
    std::cin >> t;

    for (; t > 0; t--) {
        std::string s;
        std::cin >> s;

        std::cout << s << ' ';

        while (s != "4" && s != "1") {
            int curr = 0;
            for (const auto& c : s) {
                int temp = c - '0';
                curr += temp * temp;
            }
            s = std::to_string(curr);
            std::cout << s << ' ';
        }

        std::cout << '\n';
    }

    return 0;
}