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 <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define PREP(it,array) for(vector<P>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int m;
  while( cin>>m, m ){
    int a[7],b[7];
    REP(i,m){
      int aa,bb;
      cin >>aa>>bb;
      a[i] = aa;
      b[i] = bb;
    }

    int dp[m+1][1000+1];
    memset( dp, 0, sizeof(dp) );
    dp[0][0] = 1;

    REP(i,m){
      REP(j,1001){
	REP(k,b[i]+1){
	  if( j+a[i]*k <= 1000 )
	    dp[i+1][j+a[i]*k] += dp[i][j];
	}
      }
    }

    int g;
    cin >> g;
    REP(_t,g){
      int n;
      cin >> n;
      cout << dp[m][n] << endl;
    }


  }

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;


int TotalMethodOfCard[8][1001];
int Number[7], Count[7];

int main() {
	int m, g, n;

	while (cin >> m, m) {
		memset(TotalMethodOfCard, 0, sizeof(TotalMethodOfCard));
		memset(Number, 0, sizeof(Number));
		memset(Count, 0, sizeof(Count));

		for (int i = 0; i < m; i++) {
			cin >> Number[i] >> Count[i];
		}

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


	return 0;
}

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

using namespace std;

void dfs(int pos, int acc, int lim, const vector<pair<int,int> > &cards, vector<int> &patterns) {
    const int N = cards.size();
    if(acc > lim) return;
    if(pos == N) {
        patterns[acc]++;
        return;
    }
    for(int i = 0; i <= cards[pos].second; ++i) {
        dfs(pos+1, acc+cards[pos].first*i, lim, cards, patterns);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true) {
        int N;
        cin >> N;
        if(!N) break;
        vector<pair<int,int> > cards(N);
        for(int i = 0; i < N; ++i) {
            cin >> cards[i].first >> cards[i].second;
        }
        vector<int> patterns(1001, 0);
        dfs(0, 0, 1000, cards, patterns);

        int G;
        cin >> G;
        while(G--) {
            int NG;
            cin >> NG;
            cout << patterns[NG] << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;

int n,m;
int a[8],b[8];
int dp[8][1001];

void solve(void){
	memset(dp,0,sizeof(dp));
	dp[0][0] = 1;
	for(int i=0;i<n;i++){
		for(int j=0;j<=1000;j++) if(dp[i][j]>0) {
			for(int k=0;k<=b[i] && j+k*a[i]<=1000;k++){
				dp[i+1][j+a[i]*k] += dp[i][j];
			}
		}
	}
}

int main(void){
	while(cin>>n,n){
		for(int i=0;i<n;i++){
			cin>>a[i]>>b[i];
		}

		solve(); //DPツ表ツ青カツ青ャ

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

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;


signed main(){
    int m;
    while(cin >> m, m){
        vi a(m), b(m);
        rep(i, 0, m){
            cin >> a[i] >> b[i];
        }
        int dp[100][1001] = {0};
        dp[0][0] = 1;
        rep(i, 1, m + 1) {
            //cout << " " << a[i - 1] << " " << b[i - 1] <<endl;
            rep(k, 0, 1000){
                dp[i][k] = dp[i - 1][k];
            }
            rep(j, 1, b[i - 1] + 1){
                rep(k, 0, 1000){
                    if(k - a[i - 1]*j >= 0){
                        dp[i][k] += dp[i - 1][k - a[i - 1]*j];
                    }
                }
            }
            /*cout << " " << i << " ";
            rep(j, 0, 15){
                cout<<dp[i][j]<<" ";
            }
            cout << endl;*/
        }
        /*rep(i, 0, 15){
            cout<<dp[m][i]<<" ";
        }
        cout<<endl;*/
        int g;
        cin >> g;
        rep(i, 0, g){
            int n;
            cin >> n;
            cout << dp[m][n] <<endl;
        }
    }
}

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

using namespace std;

//DP(®Ivæ@)
int main() {

  while(1) {

    int m; //J[hÌíÞ
    int a[10],b[10]; //J[hÌÆ»Ì
    int g; //Q[ñ
    int n; //aÉµ½¢
 
    int dp[8][1001]; //DPe[u

    cin >> m;

    if(m==0) break;

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

    cin >> g;


    //DPe[uð0Åú»
    memset(dp,0,sizeof(dp));

    //0ÔÚÜÅÌJ[hðgÁÄ0ðìéû@Í1Êè
    dp[0][0]=1;

    //·×ÄÌíÞÌJ[hÉÂ¢Ä
    for(int i=0;i<m;i++) {

      //0`1000ÜÅÌ·×ÄÌ®ðìéû@ðDPe[uÉLq
      for(int j=0;j<=1000;j++) {

	//i+1ÔÚÜÅÌJ[hðgÁÄjðìé½ßÉÍAiÔÚÜÅÌJ[hðgÁÄj-k*a[i](½¾µ0<=k<=b[i],0<=k*a[i]<=jð½·)ðìé±ÆªoÈ¯êÎÈçÈ¢
	for(int k=0;k<=b[i] && k*a[i]<=j;k++) {

	  //±êð«µÄ¢­±ÆÅAi+1ÔÚÜÅÌJ[hðgÁÄjðìéû@ÌêÌªßçêé
	  dp[i+1][j]+=dp[i][j-k*a[i]];

	}

      }

    }

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

      cin >> n;

      //æÉe[uðìÁÄ¢éÌÅA·ÅÉdp[m][n]ÉÍAmÔÚÜÅÌJ[hðgÁÄnðìéû@ÌêÌA·Èí¿¡ñÌ¦ªL³êÄ¢é
      cout << dp[m][n] << endl;
	
    }

  }

  return 0;

}

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;
}