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])
		}
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int res[10][2000];

int main() {
	int n;
	while(n=in()){
		int i;
		int a,b;
		memset(res,0,sizeof(res));
		res[0][0]=1;
		for(i=0;i<n;i++){
			cin>>a>>b;
//cout<<"piyo"<<endl;
			int j,k;
			for(j=0;j<=1000;j++){
//cout<<i<<" "<<j<<endl;
				for(k=0;k<=b;k++){
					if(0>j-a*k)break;
					res[i+1][j]+=res[i][j-a*k];
				}
			}
		}
//cout<<"hoge"<<endl;
		int m=in();
		for(i=0;i<m;i++){
			int j;
			cin>>j;
			cout<<res[n][j]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

const int MAX_M = 7, MAX_B = 10, MAX_N = 1000;
int m,g,n;
int a[MAX_M], b[MAX_M];
int dp[MAX_M + 1][MAX_N + 1];

int main()
{
    while(cin >> m && m) {
        for(int i = 0; i < m; i++)
            cin >> a[i] >> b[i];

        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;

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

        
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,ans,m;
struct P{
	int x,y;
};
P tab[10000];
int comp(const P &a,const P &b){
	return a.x<b.x;
}
void rec(int now,int sum){
	if(sum==n){
		ans++;
		return ;
	}
	if(now==m)
	return ;
	for(int i=0;i<=tab[now].y&&sum+tab[now].x*i<=n;i++){
		rec(now+1,sum+tab[now].x*i);
	}
}
void solve(){
	rec(0,0);
}
int main(){
	int g,s,t;
	while(cin>>m,m){
		for(int i=0;i<m;i++){
			cin>>s>>t;
			tab[i].x=s;
			tab[i].y=t;
		}
		sort(tab,tab+m,comp);
		cin>>g;
		while(g-->0){
			ans=0;
			cin>>n;
			solve();
			cout<<ans<<endl;
		}
	}
	return 0;
}