<|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:  
        – Compute a = A_i mod 9  
        – Update s = (s * a) mod 9  
        – Update 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 (to hold prefix products mod 9) and ans = 0 (to hold 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 with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

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

    int K;
    cin >> K;                      // Number of test cases
    while (K--) {
        int N;
        cin >> N;                  // Length of sequence
        vector<long long> A(N);
        for (int i = 0; i < N; i++) {
            cin >> A[i];
        }

        // If the first element is zero, all prefix products are zero => S=0 => dr=0
        if (A[0] == 0) {
            cout << 0 << "\n";
            continue;
        }

        int s = 1;   // Current prefix product modulo 9
        int ans = 0; // Running sum of prefix products modulo 9

        for (int i = 0; i < N; i++) {
            // Reduce A[i] modulo 9 to keep numbers small
            int a_mod = A[i] % 9;
            // Update prefix product mod 9
            s = (s * a_mod) % 9;
            // Include it in the running sum mod 9
            ans = (ans + s) % 9;
        }

        // Convert the sum mod 9 into the final digital root:
        // - If ans == 0, then the true sum S was a multiple of 9 but nonzero => digital root = 9
        // - Otherwise digital root = ans
        if (ans == 0)
            cout << 9 << "\n";
        else
            cout << ans << "\n";
    }
    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()
```