<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

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

<|response|>
1. Abridged Problem Statement
Given K test cases. Each test case consists of an integer N (1 ≤ N ≤ 1000) and a sequence of N non-negative integers A₁…A_N (each up to 10⁹). Define
  S = A₁
    + A₁·A₂
    + A₁·A₂·A₃
    + …
    + A₁·A₂·…·A_N.
Compute the digital root of S, where the digital root of x is obtained by repeatedly summing its decimal digits until a single digit remains (0 has digital root 0).

2. Key Observations
- Digital root dr(x) is equivalent to x modulo 9, with the convention:
    • if x = 0, dr(x) = 0
    • if x > 0 and x mod 9 = 0, dr(x) = 9
    • otherwise dr(x) = x mod 9
- We only need S mod 9 to determine dr(S).
- Let P_i = A₁·A₂·…·A_i. Then S = ∑_{i=1..N} P_i. We can maintain P_i modulo 9.
- If A₁ = 0, then every P_i = 0 ⇒ S = 0 ⇒ dr(S) = 0.
- Otherwise:
    • Keep s = current prefix product P_i mod 9, initialized to 1.
    • Keep ans = running sum of these s values mod 9, initialized to 0.
    • For each A_i: a = A_i mod 9; s = (s * a) mod 9; ans = (ans + s) mod 9.
    • At the end, if ans = 0, S was a nonzero multiple of 9 ⇒ dr(S) = 9; if ans > 0, dr(S) = ans.

3. Full Solution Approach
- Read K.
- For each test case:
  1. Read N and the array A of length N.
  2. If A[0] = 0, output 0 and continue to the next test case.
  3. Initialize s = 1 (prefix products mod 9) and ans = 0 (sum mod 9).
  4. For i = 0..N−1: a_mod = A[i] mod 9; s = (s * a_mod) mod 9; ans = (ans + s) mod 9.
  5. If ans = 0, output 9; else output ans.
- This runs in O(N) per test case and uses O(1) extra memory.

4. C++ Implementation
```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;
}
```

5. Python Implementation with Detailed Comments
```python
import sys

def digital_root_of_sum_prefix_products(A):
    # If the first element is zero, then every prefix product is zero => S=0 => dr=0
    if A[0] == 0:
        return 0

    s = 1    # current prefix product modulo 9
    ans = 0  # running sum of prefix products modulo 9

    for x in A:
        x_mod = x % 9        # reduce to modulo 9
        s = (s * x_mod) % 9  # update prefix product mod 9
        ans = (ans + s) % 9  # update running sum mod 9

    # If ans == 0 then the nonzero S was divisible by 9 => digital root = 9
    return 9 if ans == 0 else ans

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    K = int(next(it))        # Number of test cases
    outputs = []

    for _ in range(K):
        N = int(next(it))    # Length of the sequence
        A = [int(next(it)) for _ in range(N)]
        dr = digital_root_of_sum_prefix_products(A)
        outputs.append(str(dr))

    print("\n".join(outputs))

if __name__ == "__main__":
    main()
```
