import Data.List
import Data.Maybe
import Data.Ord

main = interact $ unlines . map show . comb_of_numseq [] . sortBy (comparing fst) . read' . lines
 where read' = map ((\[n,s]->(n,s)) . map read . words)

comb_of_numseq _ [] = []
comb_of_numseq memo ((n,s):nss)
 | s > 330 = 0 : comb_of_numseq memo nss
 | any ((==(n,s)).fst) memo = fromJust (lookup (n,s) memo) : comb_of_numseq memo nss
 | otherwise = r : comb_of_numseq (((n,s),r):memo) nss
  where r = length $ filter (==s) $ candidate n s

candidate x s = perms 0 (map (*) [1..x]) [0..9]
 where
  perms acc [] _ = [acc]
  perms s' (f:fs) cs = concat [perms s'' fs (delete c' cs) | c'<-cs, let s'' = s' + f c', s''<=s]