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 is , the haiestra of is , and the haiestra of is because and .
To test if you were paying attention, the locals have given you the number and the sequence of of natural numbers and will ask you questions. For each question, you will receive two numbers and () and you will have to answer with a single digit, representing the haiestra of the number .
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 . The second line contains the numbers . The third line contains the natural number . The next lines each contain two numbers separated by a space.
Output data
The output file cdcq.out
will contain lines, each having a number. Each line contains the answer to question .
Constraints and clarifications
- for each from to .
# | Points | Constraints |
---|---|---|
1 | 12 | |
2 | 17 | |
3 | 6 | |
4 | 18 | |
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
, and the haiestra of is .
, and the haiestra of is .
, and the haiestra of is .