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 <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;
	char c;
	char str[1024];
	string s;
	ll l;
	int nextInt(){
		scanf("%d", &n);
		return n;
	}
	ll nextLong(){
		scanf("%lld", &l);
		return l;
	}
	char nextChar(){
		scanf("%c", &c);
		return c;
	}
	string next(){
		scanf("%s", str);
		return string(str);
	}
	string nextLine(){
		getline(cin, s);
		return s;
	}
};
Input in;

int a[15], b[15];
ll dp[15][2500];

int main(){
	while(1){
		int m = in.nextInt();
		if(m == 0) break;

		rep(i, m) a[i] = in.nextInt(), b[i] = in.nextInt();
		rep(i, 15) rep(j, 2500) dp[i][j] = 0LL;

		dp[0][0] = 1LL;
		rep(i, m){
			for(int j = 0; j < 2500; j++){
				if(dp[i][j] == 0LL) continue;
				for(int k = j, l = 0; k < 2500 && l <= b[i]; k += a[i], l++){
					dp[i + 1][k] += dp[i][j];
				}
			}
		}
		// for(int i = 0; i < m + 1; i++){
		// 	for(int j = 0; j < 26; j++){
		// 		printf("%4lld", dp[i][j]);
		// 	}
		// 	printf("\n");
		// }

		int g = in.nextInt();
		rep(i, g){
			int n = in.nextInt();
			printf("%lld\n", dp[m][n]);
		}
	}
}

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

int a[10];
int b[10];
int dp[10][1010]; // x以下のカードのみを用いて数yになるパターン数

int main() {
  int m;
  
  while( cin >> m, m ) {
    fill( a, a+8, 0 );
    fill( a, b+8, 0 );
    for( int i=0; i<m; i++ ) {
      cin >> a[i] >> b[i];
    }
    
    fill(dp[0], dp[8], 0);
    dp[0][0] = 1;
    for( int i=0; i<m; i++ ) {
      for( int j=0; j<1001; j++ ) {
	if( dp[i][j] ) {
	  for( int k=0; k<=b[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    
    int g;
    cin >> g;
    for( int i= 0; i<g; i++ ) {
      int q;
      cin >> q;
      cout << dp[m][q] << endl;
    }
  }
  
  return 0;
}

C++: #include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
#include <string>
using namespace std;

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

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

int main()
{
	int dp[10001];
	
	int m;
	while (scanf("%d", &m), m){
		dp[0] = 1;
		for (int i = 1; i <= 1000; i++){
			dp[i] = 0;
		}
		
		for (int i = 0; i < m; i++){
			int number, num;
			scanf("%d %d", &number, &num);
			
			for (int j = 1000; j >= 0; j--){
				if (dp[j]){
					for (int k = 1; k <= num; k++){
						dp[j + k * number] += dp[j];
					}
				}
				
			}
		}
		
		int g;
		scanf("%d", &g);
		for (int i = 0; i < g; i++){
			int n;
			scanf("%d", &n);
			
			printf("%d\n", dp[n]);
		}
	}
	
	return 0;
}

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

using namespace std;

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

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001];
int main() {
	int m;
	while (scanf("%d", &m), m) {
		memset(dp, 0, sizeof(dp));
		dp[0] = 1;
		rep(i, m) {
			int a, b; scanf("%d%d", &a, &b);
			for (int j = 1000; j >= 0; j--) {
				for (int k = 1; k <= b; k++) {
					if (j >= a*k)dp[j] += dp[j - a*k];
				}
			}
		}
		int g; scanf("%d", &g);
		rep(i, g) {
			int n; scanf("%d", &n);
			printf("%d\n", dp[n]);
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int n,a[10],b[10],ans;
int p;

void saiki(int ima,int sum){
  if(sum==p) ans++;
  if(ima==n||sum>=p) return;
  for(int i=0;i<=b[ima];i++) saiki(ima+1,sum+a[ima]*i);
}

int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    int m;
    cin>>m;
    while(m--){
      cin>>p;
      ans=0;
      saiki(0,0);
      cout <<ans<<endl;
    }
  }
  return 0;
}

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

int main()
{
	int m;
	while (scanf("%d", &m), m)
	{
		int a[7], b[7];
		for (int i = 0; i < m; ++i)
			scanf("%d %d", a+i, b+i);

		int combi[1001];
		memset(combi, 0, sizeof(combi));
		combi[0] = 1;
		for (int i = 0; i < m; ++i)
		{
			int c[1001];
			memset(c, 0, sizeof(c));
			for (int j = 1; j <= b[i]; ++j)
				for (int k = 1000 - j*a[i]; k >= 0; --k)
					if (combi[k])
						c[k + j*a[i]] += combi[k];
			for (int i = 0; i < 1001; ++i)
				combi[i] += c[i];
		}
		int g;
		scanf("%d", &g);
		while (g--)
		{
			int n;
			scanf("%d", &n);
			printf("%d\n", combi[n]);
		}
	}

	return 0;
}

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

int main()
{
	int m, g;
	int n, dp[8][1001];
	
	cin >> m;

	while (m > 0) {
		memset(dp, 0, sizeof(dp));

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

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

			cin >> a >> b;

			for (int j = 1; j < 1001; j++) {
				dp[i][j] = dp[i - 1][j];

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

		cin >> g;

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

		cin >> m;
	}

	return 0;
}

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

long long int dp[1001];
int a[8],b[8];
int main(void){
	int n,g;
	while(1){
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%d%d",&a[i],&b[i]);
		}
		dp[0]=1;
		for(int i=0;i<n;i++){
			for(int j=1000;j>=0;j--){
				for(int k=1;k<=b[i];k++){
					if(a[i]*k<=j){
						dp[j]+=dp[j-a[i]*k];
					}
				}
			}
		}
		scanf("%d",&g);
		for(int i=0;i<g;i++){
			int a;
			scanf("%d",&a);
			printf("%d\n",dp[a]);
		}
	}
    return 0;
}