P12595 [AOJ] Sum of Cards
普及
题目描述

Let's play the game using a bag containing several cards with integers written on it. In each game, participants first declare one of their favorite number n. Then, take out an appropriate number of cards from the bag at a time, and if the sum of the numbers written on those cards is equal to n, you will receive a luxurious prize. After each game, the cards will be returned to the bag.

Create a program that inputs the information of m types of cards in the bag and the number declared by the participants in g games, and outputs how many combinations of cards can receive luxury products in each game. please.

Input

A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:

m
a1 b1
a2 b2
::
am bm
g
n1
n2
::
ng

The number of card types m (1 ≤ m ≤ 7) on the first line, the integer ai (1 ≤ ai ≤ 100) written on the i-type card on the following m line, and the number bi (1 ≤ bi ≤ 10) Are given with a space delimiter.

The next line is given the number of games g (1 ≤ g ≤ 10), and the next g line is given the integer ni (1 ≤ ni ≤ 1,000) declared in game i.

The number of datasets does not exceed 100.

Output

For each input dataset, the i line prints the number of card combinations that will give you a gorgeous prize in Game i.

Example

Input

5
1 10
5 3
10 3
25 2
50 2
4
120
500
100
168
7
1 10
3 10
5 10
10 10
25 10
50 10
100 10
3
452
574
787
0

Output

16
0
12
7
9789
13658
17466

输入格式

见题面

输出格式

见题面

样例输入
5 1 10 5 3 10 3 25 2 50 2 4 120 500 100 168 7 1 10 3 10 5 10 10 10 25 10 50 10 100 10 3 452 574 787 0
样例输出
16 0 12 7 9789 13658 17466