Gauss

Starting from A, pay divisor-based costs to shrink the number or stay on lucky numbers, and find the cheapest exact-move cost to end at B.

Hard9Dynamic programmingShortest pathNumber theoryGraphNo attempts yetTime limit2sMemory limit256 MB

Problem

Young Carl Friedrich Gauss would not sit still in class, so his teacher invented a task to keep him busy.

The teacher fixes a sequence of positive integers F(1),F(2),,F(K)F(1), F(2), \ldots, F(K) and sets F(t)=0F(t) = 0 for every t>Kt > K. She also fixes a set of lucky numbers. If XX is a lucky number, its price is C(X)C(X).

At the start a positive integer AA is written on the board. In each move Carl does exactly one of the following.

  • The number on the board is NN. He erases it and writes a divisor MM of NN with M<NM < N. The price of this move is F(d(N/M))F(d(N/M)), where d(x)d(x) is the number of divisors of xx, counting xx itself.
  • The number on the board is NN and NN is a lucky number. He leaves NN on the board. The price of this move is C(N)C(N).

Carl must make exactly LL moves, and after the last one the number BB must be on the board. Let G(A,B,L)G(A, B, L) be the smallest total price of such a sequence of moves. If no sequence of exactly LL moves works, then G(A,B,L)=1G(A, B, L) = -1.

The teacher gives Carl QQ queries. A query gives the numbers AA and BB, and its answer is G(A,B,L1)+G(A,B,L2)++G(A,B,LM)G(A, B, L_1) + G(A, B, L_2) + \cdots + G(A, B, L_M). The numbers L1,,LML_1, \ldots, L_M are the same for every query.

Input

The first line contains an integer KK (1K100001 \le K \le 10000).
The second line contains KK integers F(1),F(2),,F(K)F(1), F(2), \ldots, F(K), each at least 1 and at most 1000.
The third line contains an integer MM (1M10001 \le M \le 1000).
The fourth line contains MM integers L1,L2,,LML_1, L_2, \ldots, L_M, each at least 1 and at most 10000.
The fifth line contains an integer TT, the number of lucky numbers (1T501 \le T \le 50).
Each of the next TT lines contains two integers XX and C(X)C(X), meaning that XX is a lucky number with price C(X)C(X) (1X1061 \le X \le 10^6, 1C(X)10001 \le C(X) \le 1000). No lucky number appears twice.
The next line contains an integer QQ (1Q500001 \le Q \le 50000).
Each of the next QQ lines contains two integers AA and BB (1A,B1061 \le A, B \le 10^6).

Output

Print QQ lines. The ii-th line contains the answer to the ii-th query.

Note

Take K=4K = 4 with F(1)=F(2)=F(3)=F(4)=1F(1) = F(2) = F(3) = F(4) = 1, the two lucky numbers 2 and 4 with C(2)=5C(2) = 5 and C(4)=10C(4) = 10, the values L1=1L_1 = 1 and L2=2L_2 = 2, and the query A=4A = 4, B=2B = 2.

For L1=1L_1 = 1 Carl has a single move, so he replaces 4 by 2 and pays F(d(4/2))=F(2)=1F(d(4/2)) = F(2) = 1. Therefore G(4,2,1)=1G(4, 2, 1) = 1.

For L2=2L_2 = 2 he has two options.

  • Replace 4 by 2, then leave 2 on the board because 2 is lucky. The price is F(d(4/2))+C(2)=1+5=6F(d(4/2)) + C(2) = 1 + 5 = 6.
  • Leave 4 on the board, then replace it by 2. The price is C(4)+F(d(4/2))=10+1=11C(4) + F(d(4/2)) = 10 + 1 = 11.

The first option is cheaper, so G(4,2,2)=6G(4, 2, 2) = 6 and the answer to the query is 1+6=71 + 6 = 7.