p118.ans1
======================
5

=================
p118.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<int> a;

void read() {
    cin >> n;
    a.resize(n);
    cin >> a;
}

void solve() {
    // The digital root of a positive number equals 1 + (n - 1) mod 9, i.e. its
    // value mod 9 with 0 mapped to 9. So we evaluate the nested sum
    // A1 + A1*A2 + ... + A1*...*AN modulo 9: keep a running prefix product s
    // mod 9 and accumulate it. The only special case is A1 == 0, where the
    // whole expression is 0 and the digital root is 0.

    if(a[0] == 0) {
        cout << 0 << '\n';
        return;
    }

    int ans = 0, s = 1;
    for(int i = 0; i < n; i++) {
        s = s * (a[i] % 9) % 9;
        ans = (ans + s) % 9;
    }

    cout << (ans == 0 ? 9 : ans) << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p118.in1
======================
1
3 2 3 4

=================
statement.txt
======================
118. Digital Root

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit number then it is a digital root for n and otherwise digital root of n is equal to digital root of f(n). For example, digital root of 987 is 6. Your task is to find digital root for expression A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1.


Input

Input file consists of few test cases. There is K (1<=K<=5) in the first line of input. Each test case is a line. Positive integer number N is written on the first place of test case (N<=1000). After it there are N positive integer numbers (sequence A). Each of this numbers is non-negative and not more than 109.


Output

Write one line for every test case. On each line write digital root for given expression.


Sample Input

1
3 2 3 4
Sample Output

5
Author	: Michael R. Mirzayanov
Resource	: PhTL #1 Training Contests
Date	: Fall 2001

=================
