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 <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int solve(vi& a,vi& b,int n,vi& c,int p)
{
	if(p==a.size()){
		int sum=0;
		rep(i,a.size())
			sum+=a[i]*c[i];
		return sum==n;
	}
	else{
		int res=0;
		rep(i,b[p]+1){
			c[p]=i;
			res+=solve(a,b,n,c,p+1);
		}
		return res;
	}
}

int main()
{
	for(int m;cin>>m,m;){
		vi a(m),b(m);
		rep(i,m)
			cin>>a[i]>>b[i];

		int g; cin>>g;
		rep(i,g){
			int n; cin>>n;
			vi c(m);
			cout<<solve(a,b,n,c,0)<<endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

int main(void) {

  int m;
  while( cin >> m ){
    if( m == 0 ) break;
    vector<int> a(m);
    vector<int> b(m);
    for(int i=0; i<m; i++) cin >> a[i] >> b[i];

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

  }

  return 0;
}

// EOF

C++: #include <iostream>
using namespace std;
int dp[8][1001] = {0};
int a[7],b[7],m;

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

  dp[0][0] = 1;

  for(int i=0;i<m;i++) {
    for(int j=0;j<1001;j++) {
      for(int k=0;j-k*a[i]>=0 && k <= b[i];k++) {
        dp[i+1][j] += dp[i][j-k*a[i]];
      }
      //        cout << "dp[" << i+1 << "][" << j << "] = " << dp[i+1][j] << endl;
    }
  }
}

int main() {
  int g,n;
  while(cin >> m) {
    if(m==0) break;
    for(int i=0;i<m;i++) {
      cin >> a[i] >> b[i];
    }
    comb();
    cin >> g;
    for(int i=0;i<g;i++) {
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }
}

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

const int MAXM = 8;
const int SIZE = 10001;

int m;
int a[MAXM], b[MAXM];
int dp[SIZE];

void rec(int sum, int i, int j) {
  if(i == m) {
    dp[sum] += 1;
    return;
  }
  rec(sum, i+1, 0);
  if(j < b[i]) rec(sum+a[i], i, j+1);
}

int main() {
  while(cin >> m && m) {
    for(int i = 0; i < m; ++i) {
      cin >> a[i] >> b[i];
    }
    fill(dp, dp+SIZE, 0);
    rec(0,0,0);

    int g;
    cin >> g;
    while(g--) {
      int n;
      cin >> n;
      cout << dp[n] << endl;
    }
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

#define MAX 7

#define VMAX 1000



int m, n, counter;

int value[MAX+1], amount[MAX+1];



int compute(){

    int T[MAX+1][VMAX+1];

    for ( int i = 0; i <= m; i++ )

	for ( int j = 0; j <= n; j++ ) T[i][j] = 0;

    for ( int j = 0; j <= n; j++ ) T[0][j] = 0;

    for ( int i = 0; i <= m; i++ ) T[i][0] = 1;



    for ( int i = 1; i <= m; i++ ){

      	for ( int j = 0; j <= n; j++ ) T[i][j] = T[i-1][j];

	for ( int j = 1; j <= n; j++ ){

	    for ( int a = 1; a <= amount[i]; a++ ){

		int k = j - a*value[i];

		if ( k >= 0 ) T[i][j] +=  T[i-1][k];

	    }

	}

    }



    int maxe = 0;

    for ( int i = 1; i <= m; i++ ) maxe = max(maxe, T[i][n]);

    return maxe;

}



int main(){

    while( cin >> m, m ){

	for ( int i = 0; i < m; i++ ){

	    cin >> value[i+1] >> amount[i+1];

	}

	int g; cin >> g;

	for ( int i = 0; i < g; i++ ){

	    cin >> n;

	    cout << compute() << endl;

	}

    }

    return 0;

}

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

using namespace std;

#define NUMMAX (7*100*10+1)
int main(){
 
    int m;
	int a,b;
	int g;
	int n;
	int dp[2][NUMMAX];

	while(1){
		cin>>m;
		if(m==0) break;

		for(int i=0;i<NUMMAX;i++) dp[0][i]=0,dp[1][i]=0;
		dp[0][0]=1;

		for(int i=0;i<m;i++){
			cin>>a>>b;
			for(int j=0;j<b;j++){
				for(int k=0;k<NUMMAX-a*(j+1);k++){
					if(dp[0][k]!=0) dp[1][k+a*(j+1)]+=dp[0][k];
				}
			}
			for(int j=0;j<NUMMAX;j++) dp[0][j]+=dp[1][j],dp[1][j]=0;
		}

		cin>>g;

		for(int i=0;i<g;i++){
			cin>>n;
			cout<<dp[0][n]<<endl;
		}
	}
    return 0;
}

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

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

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i )
int main(){
  int n,dp[8][1001],g,m;
  while(cin >> n , n){
    int a[n],b[n];
    fill_n(dp[0],8008,0);
    dp[0][0] = 1;
    rep(i,n) cin >> a[i] >> b[i];
    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ; j < 1001 ; j++ ){
        for(int k = 0 ; k <= b[i] ; k++){
          if( j + a[i] * k > 1000) break;
          dp[i+1][j + a[i] * k] += dp[i][j];
        }
      }
    }
    cin >> g;
    while(g--){
      cin >> m;
      cout << dp[n][m] << endl;
    }
  }
}