import Control.Applicative ((<$>), (<*>))
import Control.Monad
import Data.List
import Test.HUnit
import System.Timeout

main :: IO ()
-- main = runTestTT (TestList tests) >> return ()
main = do
  m <- getl toInt
  unless (m == 0) $ do
    cs <- replicateM m $ getl $ (\[a,b] -> (a,b)) . wrds toInt
    g <- getl toInt
    ns <- replicateM g $ getl toInt
    rs <- timeout 2000000 (return $! map (solve cs) ns)
    mapM_ print $ maybe [1..g] id rs
    main

solve :: [(Int,Int)] -> Int -> Int
solve cs n = count n cs

count :: Int -> [(Int,Int)] -> Int
count 0 [] = 1
count _ [] = 0
count p ((v,n):cs)
  | p == 0 = 1
  | p < 0 = 0
  | otherwise = sum [count (p - v * i) cs | i <- [0..n]]

toInt :: String -> Int
toInt s = read s

wrds :: (String -> a) -> String -> [a]
wrds f = map f . words

getl :: (String -> a) -> IO a
getl f = f <$> getLine

tests :: [Test]
tests = [
  "test1" ~: count 20 [(10,2)] ~?= 1,
  "test2" ~: count 120 [(1,10),(5,3),(10,3),(25,2),(50,2)] ~?= 16,
  "test3" ~: count 500 [(1,10),(5,3),(10,3),(25,2),(50,2)] ~?= 0,
  "test4" ~: count 100 [(1,10),(5,3),(10,3),(25,2),(50,2)] ~?= 12,
  "test5" ~: count 168 [(1,10),(5,3),(10,3),(25,2),(50,2)] ~?= 7
  ]