cdcq

Time limit: 0.3s Memory limit: 64MB Input: cdcq.in Output: cdcq.out

The locals of Mehedinți County define the haiestra of a number as the number obtained by replacing that number with the sum of its digits, until the number is reduced to a single digit.

For example, the haiestra of 66 is 66, the haiestra of 123123 is 1+2+3=61 + 2 + 3 = 6, and the haiestra of 80978097 is 66 because 8+0+9+7=248 + 0 + 9 + 7 = 24 and 2+4=62 + 4 = 6.

To test if you were paying attention, the locals have given you the number NN and the sequence of a1,a2,aNa_1, a_2, \dots a_N of natural numbers and will ask you QQ questions. For each question, you will receive two numbers xx and yy (xyx \leq y) and you will have to answer with a single digit, representing the haiestra of the number axax+1...aya_x \cdot a_{x+1} \cdot ... \cdot a_y.

Task

Answer correctly the questions of the locals from Mehedinți.

Input data

The first line of the input file cdcq.in contains the natural number NN. The second line contains the numbers a1,a2,aNa_1, a_2, \cdots a_N. The third line contains the natural number QQ. The next QQ lines each contain two numbers separated by a space.

Output data

The output file cdcq.out will contain QQ lines, each having a number. Each line ii contains the answer to question ii.

Constraints and clarifications

  • 1N,Q300 0001 \leq N, Q \leq 300\ 000
  • 0ai1090 \leq a_i \leq 10^9 for each ii from 11 to NN.
# Points Constraints
1 12 1N,Q2 0001 \leq N, Q \leq 2\ 000
2 17 1N2 000,1Q300 0001 \leq N \leq 2\ 000, 1 \leq Q \leq 300\ 000
3 6 1N300 000,1Q101 \leq N \leq 300\ 000, 1 \leq Q \leq 10
4 18 1N30 000,1Q30 000,ai1001 \leq N \leq 30\ 000, 1 \leq Q \leq 30\ 000, a_i \leq 100
5 47 No additional constraints

Example

cdcq.in

5
0 10 26 41 33
3
2 5
3 4
1 2

cdcq.out

6
4
0

Explanation

10264133=35178010 \cdot 26 \cdot 41 \cdot 33 = 351780, and the haiestra of 351780351780 is 66.

2641=106626 \cdot 41 = 1066, and the haiestra of 10661066 is 44.

010=00 \cdot 10 = 0, and the haiestra of 00 is 00.

Log in or sign up to be able to send submissions!