while 1:

  m = int(input())

  if m==0: break



  card = []

  total = 0

  for i in range(m):

    a, b = list(map(int, input().split()))

    card.append([a, b])

    total += a*b

  card = sorted(card)[::-1]



  B = [0]*(total+1)

  for a, b in card:

    A = B[:]

    B = [0] * (total+1)

    B[a:a*b+1:a] = [1]*b

    for i in range(total+1)[::-1]:

      if A[i]:

        for e in range(i, i+a*b+1, a):

          B[e] += A[i]



  g = int(input())

  for _ in [0] * g:

    n = int(input())

    if 0<=n<=total:

      print(B[n])

    else:

      print(0)
def In(): return int(input())

while 1:

  m = In()

  if m==0: break



  card = [0]*m

  total = 0

  for i in range(m):

    a,b = list(map(int, input().split()))

    card[i] = [a,b]

    total += a*b

  card = sorted(card)[::-1]



  B = [0]*(total+1)

  for a,b in card:

    A = B[:]

    B = [0]*(total+1)

    B[a:a*b+1:a] = [1]*b

    for i in range(total+1)[::-1]:

      if A[i]:

        for e in range(i,i+a*b+1,a): B[e] += A[i]



  for _ in [0]*In():

    n = In()

    if 0<=n<=total: print(B[n])

    else: print(0)
while 1:
    m = int(input())
    if m == 0:
        break
    card = []
    total = 0
    for i in range(m):
        a, b = list(map(int, input().split()))
        card.append([a, b])
        total += a * b
    card = sorted(card)[::-1]
    B = [0] * (total + 1)
    for a, b in card:
        A = B[:]
        B = [0] * (total + 1)
        B[a : a * b + 1 : a] = [1] * b
        for i in range(total + 1)[::-1]:
            if A[i]:
                for e in range(i, i + a * b + 1, a):
                    B[e] += A[i]
    g = int(input())
    for _ in [0] * g:
        n = int(input())
        if 0 <= n <= total:
            print(B[n])
        else:
            print(0)

def In():
    return int(input())


while 1:
    m = In()
    if m == 0:
        break
    card = [0] * m
    total = 0
    for i in range(m):
        a, b = list(map(int, input().split()))
        card[i] = [a, b]
        total += a * b
    card = sorted(card)[::-1]
    B = [0] * (total + 1)
    for a, b in card:
        A = B[:]
        B = [0] * (total + 1)
        B[a : a * b + 1 : a] = [1] * b
        for i in range(total + 1)[::-1]:
            if A[i]:
                for e in range(i, i + a * b + 1, a):
                    B[e] += A[i]
    for _ in [0] * In():
        n = In()
        if 0 <= n <= total:
            print(B[n])
        else:
            print(0)

+def In(): +    return int(input()) + + -    m = int(input()) +    m = In() -    card = [] +    card = [0] * m -        card.append([a, b]) +        card[i] = [a, b] -    g = int(input()) -    for _ in [0] * g: -        n = int(input()) +    for _ in [0] * In(): +        n = In()