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<queue>
#include<stack>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
#include<functional>
#include<deque>
#include<list>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  while(1){
    int m;
    cin >> m;
    if(m==0){
      return 0;
    }
    int a[10],b[10],g,n,i,j,k,l;
    for(i=0;i<m;i++){
      cin >> a[i] >> b[i];
    }
    cin >> g;
    for(i=0;i<g;i++){
      cin >> n;
      ll dp[10][1010];
      for(j=0;j<=m;j++){
        for(k=0;k<=n;k++){
          dp[j][k]=0;
        }
      }
      dp[0][0]=1;
      for(j=1;j<=m;j++){
        for(k=0;k<=n;k++){
          for(l=0;l<=b[j-1];l++){
            int v=a[j-1]*l;
            if(k-v>=0){
              dp[j][k]+=dp[j-1][k-v];
            }
          }
        }
      }
      printf("%lld\n",dp[m][n]);
    }
  }
  return 0;
}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXM = 7, MAXB = 10, MAXN = 1001, MAXC = 71;

int dp[MAXM + 1][MAXN + 10];//dp[i][j] i番目までのカードでjを作る場合の数
int a[MAXM];
int b[MAXM];


int m;

int main()
{
    while(cin >> m && m)
    {
        REP(m){cin >> a[i] >> b[i];}

        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;

        REP(m)rep(j,MAXN)
        {
            dp[i+1][j] += dp[i][j];
            for(int k = 1;k <= b[i];k++)if(j+ a[i]*k <= MAXN)
                dp[i+1][j+a[i] * k] += dp[i][j];

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<functional>
#include<climits>
using namespace std;
typedef pair<int,int>pii;
const int INF=1e9;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};

int a[7],b[7];
int cnt,to;
int n;
void dfs(int pos,int sum){
    if(pos==n){
        if(sum==to)cnt++;
        return;
    }
    for(int i=0;i<=b[pos];i++){
        dfs(pos+1,sum+a[pos]*i);
    }
}
int main(){
    while(scanf("%d",&n),n){
        for(int i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);
        int m;
        scanf("%d",&m);
        while(m--){
            scanf("%d",&to);
            cnt=0;
            dfs(0,0);
            printf("%d\n",cnt);
        }
    }
    return 0;
}

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

int card_value[7];
int card_num[7];
int sum1[1001];
int sum2[1001];
int sum3[1001];

void get_sum(int a, int b, int sum[], int count){
	if(a >= b){
		if(count <= 1000){
			sum[count]++;
		}
		
	}else{
		for(int i = 0; i <= card_num[a]; i++){
			get_sum(a+1, b, sum, count + card_value[a] * i);
		}
	}
}
	

int main(){
	int g;
	int m;
	while(cin >> m){
		if(m == 0) break;
		for(int i = 0;i < m; i++){
			cin >> card_value[i] >> card_num[i];
		}
		for(int i = 0; i < 1001; i++){
			sum1[i] = 0;
			sum2[i] = 0;
			sum3[i] = 0;
		}
		get_sum(0, m / 2, sum1, 0);
		get_sum(m / 2, m, sum2, 0);
		for(int i = 0; i < 1001; i++){
			for(int j = 0; j < 1001 - i; j++){
				sum3[i+j] += sum1[i]*sum2[j];
			}
		}
		cin >> g;
		for(int i = 0; i < g; i++){
			int q;
			cin >> q;
			cout << sum3[q] << endl;
		}
	}
	return 0;
}

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

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

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#define INF 1<<27
#define ESP 1e-10

using namespace std;

int main()
{
  int dp[8][1001];
  int n,m,a[1000],t[1000];

  while( cin >> m,m){

    for(int i = 0 ; i < m ; i++ )
      {
	cin >> a[i] >> t[i];
      }
    /*
      fill(dp[0],dp[8],0);
      dp[0][0] = 1;

      for(int i=0 ; i<m ; i++ )
      {
      for(int j = 0 ; j <= 1000 ; j++ )
      {
      if( dp[i][j] ){
      for(int k = 0 ; k <= t[i]  ; k++ ){
      if(j + a[i]*k <= 1000)dp[i+1][j+k*a[i]] += dp[i][j];
      }
      }
      }
      }*/
    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<=t[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    /*for(int i = 0 ; i <= 7 ; i++){
      for(int j = 0 ; j <= 20;j++  ){
      cout << dp[i][j] << " ";
      }
      cout << endl;
      }*/
    int s;
    cin >> s;
    for(int i = 0 ; i < s ; i++ )
      {
	cin >> n;
	cout << dp[m][n] << endl;
      }
  }
  return 0;
}

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

int card_value[7];
int card_num[7];
int sum1[1001];
int sum2[1001];
int sum3[1001];

void get_sum(int a, int b, int sum[], int count){
	if(a >= b){
		if(count <= 1000){
			sum[count]++;
		}
		
	}else{
		for(int i = 0; i <= card_num[a]; i++){
			get_sum(a+1, b, sum, count + card_value[a] * i);
		}
	}
}
	

int main(){
	int g;
	int m;
	while(cin >> m){
		if(m == 0) break;
		for(int i = 0;i < m; i++){
			cin >> card_value[i] >> card_num[i];
		}
		for(int i = 0; i < 1001; i++){
			sum1[i] = 0;
			sum2[i] = 0;
			sum3[i] = 0;
		}
		get_sum(0, m / 2, sum1, 0);
		get_sum(m / 2, m, sum2, 0);
		for(int i = 0; i < 1001; i++){
			for(int j = 0; j < 1001 - i; j++){
				sum3[i+j] += sum1[i]+sum2[j];
			}
		}
		cin >> g;
		for(int i = 0; i < g; i++){
			int q;
			cin >> q;
			cout << sum3[q] << endl;
		}
	}
	return 0;
}

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

#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)

typedef vector<int> vi;

vi ans;

void solve(vi const& a, vi const& k){
    int n=a.size();
    vi prev(1001);
    prev[0]=1;
    rep(i,n){
        vi cur(1001);
        rep(j,1001){
            rep(l,k[i]+1){
                int t=j+l*a[i];
                if(t<1001) cur[t]+=prev[j];
            }
        }
        swap(prev,cur);
    }
    swap(ans,prev);
}

int main(){
    int m;
    while(cin >> m,m){
        vi a(m), k(m);
        rep(i,m) cin >> a[i] >> k[i];
        solve(a,k);
        int g; cin >> g;
        rep(i,g){
            int t; cin>>t;
            cout << ans[t] << endl;
        }
    }
}