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 <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

int main() {
    cin.tie(0);
	ios::sync_with_stdio(false);

    while (true) {
        int M;
        cin >> M;
        if (M == 0) {
            break;
        }
        vector<pair<int, int>> v;
        FOR(i, 0, M) {
            int a, b;
            cin >> a >> b;
            v.emplace_back(make_pair(a, b));
        }

        vector<vector<LL>> dp(10, vector<LL>(1010, 0));
        dp[0][0] = 1;

        FOR(i, 0, v.size()) {
            int a = v[i].first;
            int b = v[i].second;

            FOR(j, 0, dp[0].size()) {
                if (dp[i][j] == 0) {
                    continue;
                }
                FOR(k, 0, b + 1) {
                    if (j + a * k < dp[0].size()) {
                        dp[i + 1][j + a * k] += dp[i][j];
                    }
                }

            }
        }

        int G;
        cin >> G;
        FOR(i, 0, G) {
            int n;
            cin >> n;
            print(dp[M][n]);
        }
    }

	return 0;
}

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int m,g;
  int card[7];
  int num[7];
  while(cin>>m){
    if(m==0) break;
    REP(i,m){
      cin>>card[i]>>num[i];
    }
    cin>>g;
    int dp[1001]={};
    dp[0] = 1;
    for(int i=0; i<m; i++){
      for(int j=1000-card[i];j>=0;j--){
        for(int k=1; k<=num[i]&&j+k*card[i]<=1000;k++){
          dp[j+k*card[i]] += dp[j];
        }
      }
    }
    while(g--){
      int n;
      cin>>n;
      cout<<dp[n]<<endl;
    }
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<cctype>
#include<iomanip>
#include<functional>
#include<numeric>
#include<map>
#include<set>
#include<complex>
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<int>ans;
vector<int>a, b;
int m;
void dandc(int x, int i) {
	if (x > 1009)return;
	if (i == m) {
		ans[x]++;
		//cout << "ans[" << x << "] = " << ans[x] << endl;
		return;
	}
	//cout << "i" << " = " << i << endl;
	for (int j = 0; j <= b[i]; j++) {
		dandc(x + j*a[i], i + 1);
	}
}
int main() {
	while (cin >> m&&m) {
		ans = vector<int>(1010, 0);
		a = b = vector<int>(m,0);
		for (int i = 0; i < m; i++) {
			cin >> a[i] >> b[i];
		}
		dandc(0, 0);
		//for (int i = 0; i < 1010; i++) {
		//	cout << i << " : " << ans[i] << endl;
		//}
		int g, n;
		cin >> g;
		while (g--) {
			cin >> n;
			cout << ans[n] << endl;
		}
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

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

static const double eps = 10e-6;

int amount[101];
int num[101];
int dp[101][1001];

int main(){
	string str;
	vector<string> data;
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;
		memset(amount,0,sizeof(amount));
		memset(num,0,sizeof(num));
		memset(dp,0,sizeof(dp));

		for(int i=0;i<n;i++){
			int a,b;
			scanf("%d %d",&a,&b);
			amount[i] = b;
			num[i] = a;
		}

		dp[0][0] = 1;


		for(int i=0;i<n;i++){		
				for(int j=0;j<=amount[i];j++){
					for(int k=0;k<=1000;k++){
					if(k - num[i]*j >= 0){
						dp[i+1][k] += dp[i][k-num[i]*j];
					}
				}
			}
		}

		int g;
		scanf("%d",&g);
		for(int i=0;i<g;i++){
			int c;
			scanf("%d",&c);
			printf("%d\n",dp[n][c]);
		}
	}
}

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

using namespace std;

int dp[8][1001];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(true) {
        int M;
        cin >> M;
        if(!M) break;
        dp[0][0] = 1;
        for(int i = 1; i <= M; ++i) {
            int A, B;
            cin >> A >> B;
            for(int j = 0; j < 1001; ++j) dp[i][j] = 0;
            for(int j = 0; j < 1001; ++j) {
                if(dp[i-1][j]) {
                    for(int k = 0; k <= B; ++k) {
                        const int next = j + A*k;
                        if(next <= 1000) dp[i][next] += dp[i-1][j];
                    }
                }
            }
        }
        int N;
        cin >> N;
        while(N--) {
            int NG;
            cin >> NG;
            cout << dp[M][NG] << endl;
        }
    }
    return 0;
}