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 <algorithm>
#include <string.h>
#include <queue>
#include <cstdio>
#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000

using namespace std;

int main(){
  int n,K;
  int a[7],m[7];
  int dp[8][1001];
  while( cin >> n,n){
    for(int i = 0 ; i < n ; i++ )cin >> a[i] >> m[i];
    fill(dp[0],dp[8],0);
    dp[0][0] = 1;

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

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int dp[8][2201],a,b,n,m;
main(){
  while(cin>>n,n){
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    r(i,n){
      cin>>a>>b;
      r(j,1001)r(k,b)dp[i+1][j+(k+1)*a]+=dp[i][j];
      r(j,1001)dp[i+1][j]+=dp[i][j];
    }
    cin>>m;
    while(m--){
      cin>>a;
      cout<<dp[n][a]<<endl;
    }
  }
}

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 <bits/stdc++.h>
 
using namespace std;
 
#define MAX 7
typedef pair<int,int> P;
 
int m, cnt;
vector<P> v;
 
void check(int num, int pos)
{
    if (num == 0) {
	cnt++;
	return;
    } else if(num < 0 || m <= pos) {
	return;
    }
 
    for (int i = 0; i < v[pos].second; i++) {
	check(num-v[pos].first*(i+1), pos+1);
    }
    check(num, pos+1);
}
 
int main()
{
    int g, num;
    while (cin >> m, m) {
	v.clear(); v.resize(m);
	for (int i = 0; i < m; i++) {
	    cin >> v[i].first >> v[i].second;
	}
 
	cin >> g;
	for (int i = 0; i < g; i++) {
	    cin >> num;
	    cnt = 0; check(num, 0);
	    cout << cnt << endl;
	}
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <string>

using namespace std;

const int MAX_M = 7, MAX_B = 10, MAX_N = 1000;
int m,g,n;
int a[MAX_M], b[MAX_M];
int dp[MAX_M + 1][MAX_N + 1];

int main()
{
    while(cin >> m && m) {
        for(int i = 0; i < m; i++)
            cin >> a[i] >> b[i];

        dp[0][0] = 1;

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

        
    }
}

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

struct Card{
  int value,num;
  Card(int a, int b){value=a;num=b;};
};

int main()
{
  int m;
  int a,b;
  int g;
  int n;

  while(cin>>m,m){
    vector<Card>card;
    vector<int>dp(7000,0),dp2(7000,0);

    for(int i=0;i<m;i++){
      cin>>a>>b;
      card.push_back(Card(a,b));
    }

    dp[0]=1;
    for(int i=0;i<m;i++){
      dp2=dp;
      for(int j=0;j<=1000;j++){
	for(int k=1;k<=card[i].num;k++){
	  dp[j + card[i].value * k]+=dp2[j];
	}
      }
    }

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

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

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

using namespace std;

int cnt;
vector<pair<int,int> > card;

int main()
{
	int n;
	int dp[8][1001];
	while(cin >> n && n)
	{
		card.clear();
		int a,b;

		rep(i,n)
		{
			cin >> a >> b;
			card.push_back(make_pair(a,b));
		}
		
		memset(dp,0,sizeof(dp));
		dp[0][0] = 1;
		
		rep(i,n)
		{
			rep(j,1001)
			{
				rep(k,card[i].second+1)
				{
					if(j + card[i].first*k <= 1000) dp[i+1][j+card[i].first*k] += dp[i][j];
				}
			}
		}

		int g;
		cin >> g;

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

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

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

int main() {
  int m;
  
  while( cin >> m, m ) {
    fill( a, a+10, 0 );
    fill( a, b+10, 0 ); 
    for( int i=0; i<m; i++ ) {
      cin >> a[i] >> b[i];
    }
    
    fill(dp[0], dp[10], 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>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int m, g, s;
	while(cin >> m, m) {
		int a, b, dp[1010][7] = {};
		rep(i,m) {
			cin >> a >> b;
			rep2(j,1,b+1) {
				dp[a*j][i] = 1;
			}
			if(i!=0) {
				rep2(k,1,1001) {
					if(dp[k][i-1] != 0) {
						rep(l,b+1) {
							if(k+a*l <= 1000) dp[k+a*l][i] += dp[k][i-1];
						}
					}
				}
			}
		}
		cin >> g;
		rep(i,g) {
			cin >> s;
			cout << dp[s][m-1] << endl;
		}
	}
	return 0;
}

C++: #include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define PREP(it,array) for(vector<P>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int m;
int card[100+1];
int g;
int memo[1001][101];

int solve(int n, int u){
  if( n == 0 )
    return 1;
  if( n<0 || u>100 ) return 0;

  if( memo[n][u] != -1 )
    return memo[n][u];

  int res=0;
  REP(i,card[u]+1){
    res += solve( n-u*i, u+1 );
  }

  return memo[n][u] = res;
}


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  while( cin>>m, m ){
    REP(i,1001)
      fill( memo[i], memo[i]+101, -1 );
    memset( card,0,sizeof(card) );
    REP(_t,m){
      int a,b;
      cin >>a>>b;
      card[a] = b;
    }

    cin >> g;
    REP(_t,g){
      int n;
      cin >> n;
      cout << solve(n, 0) << endl;
    } 
  }

  return 0;
}