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 <vector>
#include <cstring>
using namespace std;

const int MEMO_MAX = 7000;

int main()
{
	int memo[ MEMO_MAX ];
	int m;

	while( cin >> m && m ){
		memset( memo, 0, sizeof(memo) );
		vector< pair< int, int > > cardList;
		for( int i = 0; i < m; ++i ){
			int figure, pieces;
			cin >> figure >> pieces;
			cardList.push_back( make_pair( figure, pieces ) );
		}
		int sumMax = 0;
		for( int i = 0; i < m; ++i ){
			int number = cardList[ i ].first;
			int pieces = cardList[ i ].second;
			int high = sumMax;
			int tmpMemo[ 7000 ];
			memset( tmpMemo, 0, sizeof(tmpMemo) );
			for( int j = 1; j <= pieces; ++j ){
				int sumMono = number * j;
				for( int k = 1; k <= high; ++k ){
					int sum = k + sumMono;
					tmpMemo[ sum ] += memo[ k ];
					if( sumMax < sum ){
						sumMax = sum;
					}
				}
				++tmpMemo[ sumMono ];
				if( sumMax < sumMono ){
					sumMax = sumMono;
				}
			}
			for( int j = 1; j <= sumMax; ++j ){
				memo[ j ] += tmpMemo[ j ];
			}
		}

		int g;
		cin >> g;
		for( int i = 0; i < g; ++i ){
			int d;
			cin >> d;
			cout << memo[ d ] << endl;
		}
	}

	return 0;
}

C++: #include <iostream>

int numOfWays(int a[], int b[], int m, int n, int index)
{
	if (n<0) return 0;
	if (n==0) return 1;
	if (index == m) return 0;
	int res = 0;
	for (int k=0; k<=b[index]; k++) {
		res += numOfWays(a, b, m, n - k*a[index], index+1);
	}
	return res;
}

int main()
{
	int m;
	while (true) {
		std::cin >> m;
		if (m==0) break;
		int a[7], b[7];
		for (int i=0; i<m; i++) std::cin >> a[i] >> b[i];
		int g; std::cin >> g;
		for (int i=0; i<g; i++) {
			int n;
			std::cin >> n;
			std::cout << numOfWays(a, b, m, n, 0) << std::endl;
		}
	}
	return 0;
}

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

int main(void){
  int m;
  while(1){
    cin>>m;
    if(m==0)break;
    map<int,int> card;
    vector<int> A;
    int a,b;
    for(int i=0;i<m;i++){
      cin>>a>>b;
      A.push_back(a);
      card[a]=b;
    }
    for(int i=m;i<7;i++){
      A.push_back(0);
      card[0]=0;
    }
    int max=0;
    for(int i=0;i<m;i++){
      max+=A[i]*card[A[i]];
    }
    map<int,int> pattern;
    for(int i=0;i<max+1;i++){
      pattern[i]=0;
    }
    int cost;
    for(int i0=0;i0<=card[A[0]];i0++){
      for(int i1=0;i1<=card[A[1]];i1++){
        for(int i2=0;i2<=card[A[2]];i2++){
          for(int i3=0;i3<=card[A[3]];i3++){
            for(int i4=0;i4<=card[A[4]];i4++){
              for(int i5=0;i5<=card[A[5]];i5++){
                for(int i6=0;i6<=card[A[6]];i6++){
                  cost=i0*A[0]+i1*A[1]+i2*A[2]+i3*A[3]+i4*A[4]+i5*A[5]+i6*A[6];
                  pattern[cost]=pattern[cost]+1;
                }
              }
            }
          }
        }
      }
    }
    int g;
    cin>>g;
    int n;
    for(int i=0;i<g;i++){
      cin>>n;
      cout<<pattern[n]<<endl;
    }
  }
  return 0;

}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int m;
  while (cin >> m, m) {
    int a[7], b[7];
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i];
    }
    int dp[8][1010] = {};
    dp[0][0] = 1;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j <= 1000; j++) {
        for (int k = 0; k <= b[i] && j - a[i] * k >= 0; k++) {
          dp[i + 1][j] += dp[i][j - a[i] * k];
        }
      }
    }
    int g;
    cin >> g;
    for (int i = 0; i < g; i++) {
      int n;
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }

  return 0;
}

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

using namespace std;

int main()
{
	int N, G, A, B;

	while (true)
	{
		cin >> N;

		if (N == 0) { break; }

		vector<vector<int> > dp(N + 1, vector<int>(1001, 0));

		dp[0][0] = 1;

		for (int i = 1; i <= N; i++)
		{
			cin >> A >> B;

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

		cin >> G;

		for (int i = 0; i < G; i++)
		{
			cin >> A;

			cout << dp[N][A] << endl;
		}
	}

	return 0;
}

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

int main()
{
	while (true)
	{
		int m;
		cin >> m;
		if (m==0) break;
		
		int dp[8][7001];
		dp[0][0] = 1;
		for (int i=1; i<=7000; i++) dp[0][i]=0;
		
		for (int i=1; i<=m; i++)
		{
			int a, b;
			cin >> a >> b;
			
			for (int j=0; j<=7000; j++)
				dp[i][j] = dp[i-1][j];
			
			for (int j=0; j<=7000; j++)
				if (dp[i-1][j]!=0)
					for (int k=1; k<=b; k++)
						dp[i][j+a*k] = dp[i-1][j];
		}
		
		int g;
		cin >> g;
		for (int i=0; i<g; i++)
		{
			int n;
			cin >> n;
			cout << dp[m][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];
        }

        vector<int> dp(1001, 0);
        dp[0] = 1;
        FOR(i, 0, m) {
            for(int j = 1000; j >= 0; --j){
                FOR(k, 1, B[i] + 1) {
                    int c = j + A[i] * k;
                    if(c <= 1000) {
                        dp[c] += dp[j];
                    }
                }
            }
        }

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


    return 0;
}