Problem Description
Given an integer and an array of integers. The process of constructing the array is defined as follows.
Initially, the array is empty. The process is carried out in consecutive rounds. In the -th round, the new array is defined as the concatenation of the previous version of the array , the element , and again the previous version of the array .
Formally, in each -th round, the array is updated according to the rule:
,
where the symbol «» denotes the concatenation of arrays, and is an array consisting of a single element .
After the construction of the array is completed, it is necessary to process queries. Each query is defined by a pair of integers . For each query, it is required to find the sum of the elements of the array from position to position inclusive modulo .
Input format
In the first line, an integer is given --- the number of elements in the array .
In the second line, integers are given --- the elements of the array .
In the third line, an integer is given --- the number of queries.
In the following lines, pairs of integers and are given .
Output format
For each query, output a single number --- the sum of the elements of the array on the segment modulo . Output each answer on a separate line.
Scoring
| Subtask | Additional constraints | Points | Required Subtasks |
|---|---|---|---|
| Sample | — | ||
| All elements of the array are equal to each other |
Examples
3 2 0 5 6 1 4 1 7 3 5 6 6 3 7 4 7
9 13 9 0 11 9