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 rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

#define INF 999999

int m;
int num[7];
int maisu[7];
int g;
int dp[8][1001];
int n;

signed main()
{
	while (true)
	{
		cin >> m;
		if (m == 0) break;

		rep(i,8) rep(j,1001) dp[i][j] = 0;

		rep(i,m)
		{
			cin >> num[i] >> maisu[i];
		}

		dp[0][0] = 1;
		/*
		for (int i = num[0]; i <= num[0] * maisu[0]; i += num[0])
		{
			dp[0][i] = 1;
		}
		*/

		rep(i, m)
		{
			rep(j,1001)
			{
				rep(k,maisu[i] + 1)
				{
					if (j + num[i] * k <= 1000)
					{
						dp[i + 1][j + num[i] * k] += dp[i][j];
					}
				}
			}
		}

		cin >> g;

		rep(i,g)
		{
			cin >> n;
			printf("%d\n", dp[m][n]);
		}
	}
}

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

int DP[8][1001];
int m,n,a,b,g;


int main(){
	for(int j=0; j<1001; j++)
		DP[0][j] = 0;
	DP[0][0] = 1;
	
	while(true){
		cin >> m;
		if(m == 0)
			return 0;
		
		for(int i=1; i<=m; i++){
			cin >> a >> b;
			
			for(int j=0; j<1001; j++)
				DP[i][j] = DP[i-1][j];
			
			for(int j=0; j<1001; j++){
				if(DP[i-1][j] == 0)
					continue;
				for(int k=1; k<=b; k++){
					if(k*a+j > 1000)
						break;
					DP[i][k*a+j] += DP[i-1][j];
				}
			}
			
		}
		
		cin >> g;
		for(int i=0; i<g; i++){
			cin >> n;
			cout << DP[m][n] << endl;
		}
	}
}

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;
int ans[1010] = {};
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) {
		a = b = vector<int>(m);
		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++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int ans[100*1000*10];

int nCk(int n,int k){
  long long ret = 1;
  REP(i,k) ret*=(n-i);
  REP(i,k) ret/=(i+1);
}

int main(){
  int n;
  while(scanf("%d",&n),n){
    pair<int,int> card[n];
    int mm = 0;
    REP(i,n) scanf("%d%d",&(card[i].f),&(card[i].s));
    sort(card,card+n);
    memset(ans,0,sizeof(ans));
    ans[0] = 1;
    REP(i,n){
      for(int j=mm; j>=0; j--){
	if(ans[j] != 0){
	  for(int k=1; k<=card[i].s; k++){
	    //printf("j=%d, j+k*card[i].f=%d ans[j]=%d\n",j,j+k*card[i].f,ans[j]);
	    ans[j+k*card[i].f] += ans[j];
	  }
	  mm = max(mm, j+card[i].s*card[i].f);
	}
      }
    }
    int g; cin>>g;
    REP(i,g){
      int gg; cin>>gg;
      cout << ans[gg] << 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;
}

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 bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
inline double distance(double y1, double x1, double y2, double x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (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> dy = { 0, 1, 0, -1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;


void solve(vector<pair<int, int>> v, vector<int> q) {
    VV<LL> dp(v.size() + 1, V<LL>(1001, 0));
    dp[0][0] = 1;

    FOR(i, 0, v.size()) {
        FOR(j, 0, dp[0].size()) {
            if (dp[i][j] == 0) { continue; }

            FOR(k, 0, v[i].second + 1) {
                int x = v[i].first * k;
                if (j + x < dp[0].size()) {
                    dp[i + 1][j + x] += dp[i][j];
                }
            }
        }
    }

    FOE(n, q) {
        print(dp[dp.size() - 1][n]);
    }
}

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(_, 0, M) {
            int a, b;
            cin >> a >> b;
            v.emplace_back(make_pair(a, b));
        }

        vector<int> q;
        int G;
        cin >> G;
        FOR(_, 0, G) {
            int n;
            cin >> n;
            q.emplace_back(n);
        }

        solve(v, q);
    }
    return 0;
}