Go: package main

import (
	"bufio"
			"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		m, _ := strconv.Atoi(scanner.Text())
		if m == 0 {
			break
		}
		ab := make([][]int, 0)
		for i := 0; i < m; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			buf := make([]int, len(input))
			for j := range buf {
				buf[j], _ = strconv.Atoi(input[j])
			}
			ab = append(ab, buf)
		}
		dp := make([][]int, 8)
		for i := range dp {
			dp[i] = make([]int, 1001)
		}
		dp[0][0] = 1
		for i := 1; i < m+1; i++ {
			for j := 0; j < 1001; j++ {
				if dp[i-1][j] == 0 {
					continue
				}
				s := j
				for k := ab[i-1][1]; k > -1; k-- {
					if s < 1001 {
						dp[i][s] += dp[i-1][j]
					}
					s += ab[i-1][0]
				}
			}
		}
		scanner.Scan()
		g, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < g; i++ {
			scanner.Scan()
			n, _ := strconv.Atoi(scanner.Text())
			fmt.Println(dp[m][n])
		}
	}
}

Python: def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break


while True:
    m = int(input())
    if m == 0:
        break

    a = [0 for i in range(7)]
    b = [0 for i in range(7)]
    for i in range(m):
        a[i],b[i] = [int(j) for j in input().split()]

    dp = [[0 for i in range(1001)] for j in range(8)]
    dp[0][0] = 1
    for i in range(7):
        for j in range(1001):
            for k in range(b[i] + 1):
                if j + a[i]*k <= 1000:
                    dp[i+1][j + a[i]*k] += dp[i][j]

    g = int(input())
    for i in range(g):
        n = int(input())
        print(dp[m][n])

Python: while True:
  m = int(input())
  if m == 0:
    break

  nums = []
  for _ in range(m):
    a, b = map(int, input().split())
    lst = [a * i for i in range(b + 1)]
    nums.append(lst)
  
  """
  dp[x][y] ... x種類目まででyを作る場合の数
  dp[x][y] = sum(dp[x - 1][y - wk] for wk in nums[x])
  """
  
  def solve(n):
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(m + 1):
      dp[i][0] = 1
    for x in range(1, m + 1):
      for y in range(1, n + 1):
        dp[x][y] = sum([dp[x - 1][y - v] for v in nums[x - 1] if v <= y])
    print(dp[m][n])
    
  g = int(input())
  for _ in range(g):
    solve(int(input()))

Python: # AOJ 0154 Sum of Cards
# Python3 2018.6.19 bal4u

MAX = 1001

while True:
	m = int(input())
	if m == 0: break
	ab = [0]
	for i in range(m):
		ab.append(list(map(int, input().split())))
	
	dp = [[0 for j in range(MAX)] for r in range(8)]
	dp[0][0] = 1
	for i in range(1, m+1):
		for j in range(MAX):
			if dp[i-1][j] == 0: continue;
			s = j
			for k in range(ab[i][1], -1, -1):
				if s < MAX: dp[i][s] += dp[i-1][j]
				s += ab[i][0]
	
	for i in range(int(input())):
		print(dp[m][int(input())])

Python: while 1:
    n=int(input())
    if n==0:break
    dp=[1]+[0]*3000
    for i in range(n):
        a,b=map(int,input().split())
        for j in range(1001,-1,-1):
            for k in range(1,b+1):
                dp[j+a*k]+=dp[j]
    for _ in range(int(input())):
        print(dp[int(input())])

Python: from itertools import combinations as C
while True:
    m = int(input())
    if m==0:break
    card = []
    total = 0
    for _ in range(m):
        a, b = map(int, input().split())
        card += [a]*b
        total += b

    g = int(input())
    for _ in range(g):
        ans = 0
        duplicate = []
        n = int(input())
        for i in range(1, total+1):
            for l in list(C(card, i)):
                if sum(l)==n and not l in duplicate:
                    ans += 1
                    duplicate.append(l)
        print(ans)

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write
ans = []
while 1:
    M = int(readline())
    if M == 0:
        break
    P = [list(map(int, input().split())) for i in range(M)]
    memo = {}
    def dfs(i, rest):
        if i == M:
            return rest == 0
        key = (i, rest)
        if key in memo:
            return memo[key]
        res = 0
        a, b = P[i]
        for j in range(0, b+1):
            if rest - j*a < 0:
                break
            res += dfs(i+1, rest - j*a)
        memo[key] = res
        return res
    G = int(input())
    for i in range(G):
        ans.append(str(dfs(0, int(input()))))
write("\n".join(ans))
write("\n")

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0154

"""
import sys
from sys import stdin
input = stdin.readline


def solve(m, cards, g, guesses):
    ans = []

    cards.append([0, 0])
    cards.sort()
    W = max(guesses) + 1

    dp = [0] * W
    dp[0] = 1
    for i in range(1, m+1):
        card_num = cards[i][0]
        card_rem = cards[i][1]
        for j in range(W-1, 0, -1):
            for k in range(1, card_rem+1):
                if card_num*k <= j:
                    dp[j] += dp[j-card_num*k]

    for gg in guesses:
        ans.append(dp[gg])
    return ans


def main(args):
    # m = 5
    # cards = [[1, 10], [5, 3], [10, 3], [25, 2], [50, 2]]
    # g = 4
    # guesses = [120, 500, 100, 168]

    while True:
        m = int(input())
        if m == 0:
            break
        cards = []
        for _ in range(m):
            a, b = map(int, input().split())
            cards.append([a, b])
        g = int(input())
        guesses = [int(input()) for _ in range(g)]
        ans = solve(m, cards, g, guesses)
        print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Python: while 1:
    n=int(input())
    if n==0:break
    dp=[1]+[0]*3000
    for i in range(n):
        a,b=map(int,input().split())
        for j in range(1001,-1,-1):
            for k in range(1,b+1):
                dp[j+a*k]+=dp[j]
    for _ in range(int(input())):
        print(dp[int(input())])

C++: #include<iostream>
using namespace std;
int m,g,a,sum;
int t[8],u[8];
int dp[8][1001];
void init();
void make_table();
int main(){
  while(cin>>m&&m){
    for(int i=1;i<=m;i++)cin>>t[i]>>u[i];
    make_table();
    cin>>g;
    for(int i=0;i<g;i++){
      cin>>a;
      cout<<dp[m][a]<<endl;
    }
  }
  return 0;
}
void init(){
  for(int i=0;i<8;i++){
    for(int j=0;j<1001;j++){
      if(i==0||j==0)dp[i][j]=1;
      else dp[i][j]=0;
    }
  }
  
}
void make_table(){
  init();
  
  for(int i=1;i<=u[1];i++)dp[1][t[1]*i]=1;


  for(int i=2;i<=m;i++){
    for(int j=1;j<=1000;j++){
      
      for(int k=0;k<=u[i];k++){
	if(j-t[i]*k>=0)dp[i][j]+=dp[i-1][j- t[i]*k ];
      }
      
    }
  }
  /*
  for(int i=1;i<=m;i++){
    for(int j=1;j<=30;j++){
      if(dp[i][j]<10)cout<<' ';
      cout<<dp[i][j]<<' ';
    }
    cout<<endl;
  }
  */
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int dp[10][1010];
        memset(dp, 0, sizeof(dp));
        int a[10], m[10];
        for(int i=0; i < n; i++) cin >> a[i] >> m[i];

        dp[0][0] = 1;
        for(int i=0; i < n; i++){
            for(int j=0; j <= 1000; j++){
                for(int k=0; k <= m[i] && k*a[i]+j <= 1000; k++){
                    dp[i+1][j+a[i]*k] += dp[i][j];
                }
            }
        }
        int g;
        cin >> g;
        for(int i=0; i < g; i++){
            int p;
            cin >> p;
            cout << dp[n][p] << endl;
        }
    }
}

C++: #include<iostream>

using namespace std;

int main(void){
  int m,a[8],b[8],g,n,dp[8][1001];

  while(cin >> m,m){

    for(int i=0;i<8;i++){
      for(int j=0;j<1001;j++){
	dp[i][j]=0;
      }
    }

    for(int i=0;i<m;i++)
      cin >> a[i] >> b[i];
    
    for(int j=0;j<=b[0];j++){
      dp[0][a[0]*j]++;
    }
    
    
    for(int i=0;i<m-1;i++){
      for(int j=0;j<1001;j++){
	for(int k=0;k<=b[i+1];k++){
	  if(j+(a[i+1]*k)<1001)
	    dp[i+1][j+(a[i+1]*k)]+=dp[i][j];
	}
      }
    }
    
    cin >> g;
    while(g--){
      cin >> n;
      cout << dp[m-1][n] << endl;
    }
  }

  return 0;
}