Task Gauss#11517

Task Gauss

1 second
256 megabytes
Very hard

It is a little-known story that the young Carl Friedrich Gauss was restless in class, so his teacher came up with a task to keep him preoccupied.

The teacher gave him a series of positive integers F(1), F(2), ..., F(k). We consider F(t) = 0, for t > k. Additionally, she has given him a set of lucky numbers and the price of each lucky number. If is a lucky number, then denotes its price.

Initially, a positive integer is written on the board. In each move, Carl must make one of the following things:

Carl must make exactly moves, and after he has made all his moves, the number must be written on the board. Let's denote G(a, b, l) as the minimal price with which Carl can achieve this.

If it is not possible to make such moves, we define G(a, b, l) = -1.

The teacher has given Carl queries. In each query, Carl gets numbers and and must calculate the value of G(a, b, l_1) + G(a, b, l_2) + ... + G(a, b, l_m), where l_1, l_2, ..., l_m are the same for all queries.

Input

The first line contains the positive integer k\:(1 \le k \le 10^4).

The second line contains positive integers F(1), F(2), ..., F(k) that are less than or equal to 10^3.

The following line contains the positive integer m\:(1 \le m \le 10^3).

The following line contains positive integers l_1, l_2, ..., l_m that are less than or equal to 10^4.

The following line contains the positive integer t\:(1 \le t \le 50), the total number of lucky numbers.

Each of the following lines contains numbers and that denote that is a lucky number, and is its price (1 \le x \le 10^6, 1 \le C(x) \le 10^3).

Each lucky number appears at most once.

The following line contains the positive integer q\:(1 \le q \le 50000).

Each of the following lines contains two positive integers and b\:(1 \le a, b \le 10^6).

Output

You must print lines. The -th line contains the answer to the -th query defined in the task.

Examples

l_1 = 1, so Carl can make exactly one move — replace number with number , so G(4, 2, 1) = F(d(2)) = 1.

l_2 = 2, so Carl has two options:

  • He can replace number with number and then leave number (because it's a lucky number), so he pays the price F(d(4/2)) + C(2) = 1 + 5 = 6.

  • He can leave number in the first move and replace it in the second move with number , so the price is C(4) + F(d(4/2)) = 10 + 1 = 11.

The first option costs less, so G(4, 2, 2) = 6.

The answer to the query is G(4, 2, 1) + G(4, 2, 2) = 7.

Example 1

Loading
Loading