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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int m;
    while(cin >> m, m)
    {
        vector<int> a(m), b(m);
        for(int i = 0; i < m; i++) cin >> a[i] >> b[i];
        vector<int> dp(1010, 0);
        dp[0] = 1;
        for(int i = 0; i < m; i++)
        {
            for(int j = 1010; j >= 1; j--)
            {
                for(int k = 1; k <= b[i]; k++)
                {
                    if(j - a[i] * k >= 0) dp[j] += dp[j - a[i] * k];
                }
            }
        }
        int g; cin >> g;
        for(int i = 0; i < g; i++)
        {
            int n; cin >> n;
            cout << dp[n] << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	for (int m; cin >> m&&m;) {
		int dp[1001] = { 1 };
		//????????°???????????????????????\
		//rep(i, 0, m) {
		//	int a, b; cin >> a >> b;
		//	rep(k, 0, b) {
		//		rrep(j, 0, 1001 - a) {
		//			dp[j + a] += dp[j];
		//		}
		//	}
		//	dumpc(dp);
		//}
		//????????°???????????????????????\?????????
		rep(i, 0, m) {
			int a, b; cin >> a >> b;
			rrep(j, 0, 1001) {
				if (dp[j] == 0)continue;
				rep(k, 1, b + 1) {
					if (j + a*k > 1000)break;
					dp[j + a*k] += dp[j];
				}
			}
		}
		int g; cin >> g;
		rep(i, 0, g) {
			int n; cin >> n;
			cout << dp[n] << endl;
		}
	}
	return 0;
}

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

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VB vector<bool>
#define VL vector<long long>
#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());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }

// 4近傍（右, 下, 左, 上）
const std::vector<int> dy = { 0, -1, 0, 1 };
const std::vector<int> dx = { 1, 0, -1, 0 };

using namespace std;


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

    while (true) {
        int m;
        cin >> m;
        if (m == 0) { break; }
        vector<int> A(m), B(m);
        FOR(i, 0, m) {
            cin >> A[i] >> B[i];
        }

        // dp[i][j] = i番目まで見たときに合計jを作る組み合わせの数
        vector<vector<LL>> dp(m + 1, vector<LL>(10000 + 1, 0));
        dp[0][0] = 1;

        FOR(i, 0, A.size()) {
            for (int j = dp[0].size() - 1; j >= 0; --j) {
                FOR(k, 0, B[i] + 1) {

                    // use
                    int num = A[i] * k;
                    if (j + num < dp[0].size()) {
                        dp[i + 1][j + num] += dp[i][j];
                    }
                }
            }
        }

        int g;
        cin >> g;
        FOR(i, 0, g) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
int dp[1001];
int main(){
	int m,a,b,g,n;
	while(cin>>m&&m){
		memset(dp,0,sizeof(dp));dp[0]=1;
		for(int i=0;i<m;i++){
			cin>>a>>b;
			for(int j=1000;j>=0;j--){
				for(int k=1;k<=b;k++){
					if(j+a*k>1000)break;
					dp[j+a*k]+=dp[j];
				}
			}
		}
		cin>>g;
		for(int i=0;i<g;i++){
			cin>>n;
			cout<<dp[n]<<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;
  
  int ret = 0;
  for(int i=0;i<=num[idx];i++){
    ret += solve(idx+1,sum+(card[idx]*i));
  }
  return 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;
      cout << solve(0,0) << endl;
    }
  }
}

C++: #include<iostream>
using namespace std;
int main(){
  int m,a,b,data[1001];
  while(cin>>m){
    if(m==0) return 0;
    for(int i=0;i<=1000;i++) data[i] = 0;
    data[0] = 1;
    for(int i=0;i<m;i++){
      cin >> a >> b;
      for(int k=1000;k>=0;k--){
	if(data[k]!=0){
	  for(int i=1;i<=b;i++){
	    if(a*i+k<=1000) data[a*i+k] += data[k];
	  }
	}
      }
    }
    //for(int i=0;i<10;i++) cout << data[i] << endl;
    //cout << data[2] << endl;
    int n,ansn;
    cin >> n;
    for(int i=0;i<n;i++){
      cin >> ansn;
      cout << data[ansn] << endl;
    }
  }
}