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 <stdio.h>
const int N=1e3+10;
int main(){
    int n,a,b,dp[N];
    while(true){
        scanf("%d",&n);
        if(n==0)return 0;
        dp[0]=1;
        for(int i=1;i<N;i++)dp[i]=0;
        while(n--){
            scanf("%d%d",&a,&b);
            for(int i=N-1;i>=0;i--)for(int j=1;j<=b;j++)if(j*a<=i)dp[i]+=dp[i-j*a];
        }
        scanf("%d",&n);
        while(n--){
            scanf("%d",&a);
            printf("%d\n",dp[a]);
        }
    }
}

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

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>

int a[7], b[7], m, dp[7001][7];

//次i
int rec(int n, int i){
	if(n >= 0 && i < m && dp[n][i] != -1){
		return dp[n][i];
	}

	if(n == 0){
		return 1;
	}

	if(n < 0 || i >= m){
		return 0;
	}

	int res = 0;
	for(int j=0;j<=b[i];j++){
		res += rec(n-a[i]*j, i+1);
	}

	return dp[n][i] = res;
}

int main(){
	while(std::cin >> m, m){
		for(int i=0;i<7001;i++){
			for(int j=0;j<7;j++){
				dp[i][j] = -1;
			}
		}
		for(int i=0;i<m;i++){
			std::cin >> a[i] >> b[i];
		}
		int g, n;
		std::cin >> g;
		for(;g--;){
			std::cin >> n;
			std::cout << rec(n, 0) << std::endl;;
		}
	}
}

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

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

int dp[7][7001];
int card[7], cnt[7];
int N, M;

int dfs(int pos, int now) {
	if (pos == -1) return now == 0;
	if (now < 0) return 0;
	
	if (dp[pos][now] != -1) return dp[pos][now];
	
	dp[pos][now] = 0;
	REP(i, cnt[pos] + 1) dp[pos][now] += dfs(pos - 1, now - card[pos] * i);
	
	return dp[pos][now];
}

int main() {
	while (cin >> M, M) {
		REP(i, M) scanf("%d %d", &card[i], &cnt[i]);
		cin >> N;
		
		fill(dp[0], dp[7], -1);
		REP(i, N) {
			int n;
			scanf("%d", &n);
			cout << dfs(M - 1, n) << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define int long long
int m;
int a[10];
int b[10];
int memo[1001][10];
int dp(int v,int now)
{
	if(v==0)return 1;
	if(now==m||v<0)return 0;
	if(memo[v][now]!=-1)return memo[v][now];
	int ans=0;
	for(int i=0;i<=b[now];i++)
	{
		ans+=dp(v-i*a[now],now+1);
	}
	return memo[v][now]=ans;
}
signed main()
{
	while(cin>>m&&m)
	{
		for(int i=0;i<1001;i++)for(int j=0;j<10;j++)memo[i][j]=-1;
		for(int i=0;i<m;i++)
		{
			cin>>a[i]>>b[i];
		}
		int g;cin>>g;
		for(int i=0;i<g;i++)
		{
			int n;
			cin>>n;
			cout<<dp(n,0)<<endl;
		}
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    int m, g, a, b, n;
	while(cin >> m && m)
	{	
		vector<vint> dp(m, vint(1001, 0));

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

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int m,a[7],b[7],g,n;

int f(int r,int c){
	if(c==m)return r==0;
	int res=0;
	rep(i,b[c]+1)res+=f(r-a[c]*i,c+1);
	return res;
}

int main(){
	while(cin>>m&&m){
		rep(i,m)cin>>a[i]>>b[i];
		cin>>g;
		rep(i,g){
			cin>>n;
			cout<<f(n,0)<<endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF (1<<20)
typedef pair<int,int> P;

int m,g;
int card[7],num[7];
int dp[10][10000];
int n;

int solve(int idx,int sum){
  if(sum == n) return 1;
  if(idx == m) return 0;
  if(sum > n) return 0;
  if(dp[idx][sum] != -1) return dp[idx][sum];
  
  int ret = 0;
  for(int i=0;i<=num[idx];i++){
    ret += solve(idx+1,sum+(card[idx]*i));
  }
  return dp[idx][sum] = ret;
}

int main(){
  while(cin >> m , m){
    for(int i=0;i<m;i++){
      cin >> card[i] >> num[i];
    }
    cin >> g;
    for(int i=0;i<g;i++){
      cin >> n;
      memset(dp,-1,sizeof(dp));
      cout << solve(0,0) << endl;
    }
  }
}