
object Main extends App {

  import scala.io.StdIn._
  solve
  def solve: Unit = {
    val m = readLine.trim.toInt
    if (m != 0) {
      val cards = List.tabulate(m) { _ ⇒
        val Array(a, b) = readLine.trim.split(' ').map(_.toInt)
        a → b
      }
      val pat = calPattern(cards)
      val g = readLine.trim.toInt
      for (_ ← 0 until g) {
        println(pat(readLine.trim.toInt))
      }
      solve
    }
  }
  def calPattern(cards: List[(Int, Int)]): Array[Int] = {
    def calSub(c: List[(Int, Int)], pattern: Array[Int]): Unit = {
      c match {
        case (a, b)::t ⇒ {
          for (i ← (0 to 1000).reverse) {
            if (pattern(i) > 0) {
              for (j <- 1 to b) {
                if (i + a * j <= 1000) pattern(i + a * j) += pattern(i)
              }
            }
          }
          calSub(t, pattern)
        }
        case Nil ⇒
      }
    }
    val res = Array.tabulate(1001){_ ⇒ 0}
    res(0) = 1
    calSub(cards, res)
    res
  }

}
