G - Polynomial Queries

Languages: C, C++, Java, Python, Kotlin, CSharp
Time & Memory limits: (details)

Input

Output

Sample test(s)

Input
5 3 4 2 3 1 7 2 1 5 1 1 5 2 1 5
Output
17 32