402362: GYM100738 H K-palindrome

Memory Limit:64 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

H. K-palindrometime limit per test0.5 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard output

Let's have K natural numbers: b1, b2, ..., bk. We say that X (in base 10) is K-palindrome if there exist at least one bi such as X written in base bi is a palindrome.

Answer Q queries of form: how many numbers from a range [L, U] are K-palindromes?

Input

First line of the input contains number K (1 ≤ K ≤ 13). The next line has K numbers, representing the array b (2 ≤ bi ≤ 100000). The third line contains number Q (1 ≤ Q ≤ 100000). Next Q lines contain two numbers L and U, describing queries, (0 ≤ L ≤ U ≤ 100000000).

Output

Answer each of the Q queries, one query per line.

ExamplesInput
2
2 3
2
0 10
11 15
Output
10
2

加入题单

算法标签: