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>
using namespace std;
int dp[1001];
int main(){
	int m,a,b;
	while(true){
	cin>>m;
	if(m==0)break;
	for(int i=0;i<=1000;i++)dp[i]=0;
	dp[0]=1;
	for(int u=0;u<m;u++){
		cin>>a>>b;
		for(int i=1000;i>=0;i--){
			for(int j=1;j<=b;j++){
				if(i+j*a>1000)break;
				dp[i+j*a]+=dp[i];
			}
		}
	}
	int g,n;
	cin>>g;
	for(int u=0;u<g;u++){
		cin>>n;
		cout<<dp[n]<<endl;
	}
	}
	return 0;
}

C++: // 2011/02/10 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int m;
		cin >> m;
		if(m==0)
			break;

		int DP[1001];
		DP[0] = 1;
		for(int i=1; i<=1000; i++)
			DP[i] = 0;

		for(int i=0; i<m; i++){
			int a, b;
			cin >> a >> b;

			int DP_tmp[1001];
			for(int j=0; j<=1000; j++){
				DP_tmp[j] = DP[j];
			}

			for(int j=0; j<=1000; j++){
				if(DP[j]==0)
					continue;

				for(int k=1; k<=b; k++){
					if(j+a*k>1000)
						break;

					DP_tmp[j+a*k] += DP[j];
				}
			}

			for(int j=0; j<=1000; j++){
				DP[j] = DP_tmp[j];
			}
		}

		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 <algorithm>
using namespace std;

int a[11];
int b[11];
int dp[11][1010]; // xテ、ツサツ・テ、ツクツ凝」ツ?ョテ」ツつォテ」ツδシテ」ツδ嘉」ツ?ョテ」ツ?ソテ」ツつ津ァツ板ィテ」ツ??」ツ?ヲテヲツ閉ーyテ」ツ?ォテ」ツ?ェテ」ツつ凝」ツδ妥」ツつソテ」ツδシテ」ツδウテヲツ閉ー

int main() {
  int m;
  
  while( cin >> m && m ) {
    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++: //56
#include<iostream>

using namespace std;

int a[7],b[7];

int dfs(int n,int m){
  if(m==0){
    return n==0;
  }else{
    int s=0;
    for(int i=0;i<=b[m-1];i++){
      s+=dfs(n-a[m-1]*i,m-1);
    }
    return s;
  }
}

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

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int m,a[7],b[7],g,n;

int f(int r,int c){
	if(r<0)return 0;
	if(c==m)return r==0;
	int res=0;
	rep(i,b[c]+1)res+=f(r-a[c]*i,c+1);
	return res;
}

int main(){
	while(cin>>m&&m){
		rep(i,m)cin>>a[i]>>b[i];
		cin>>g;
		rep(i,g){
			cin>>n;
			cout<<f(n,0)<<endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int a[8][1000];
int main(){
  int b;
  while(cin>>b,b){
      memset(a,0,sizeof(a));
      for(int c=0;c<b;c++){
	  a[c][0]=1;
	  int d,e;cin>>d>>e;
	  for(int f=0;f<=e;f++){
	      for(int g=max(f*d,1);g<=1000;g++){
		  a[c+1][g]+=a[c][g-f*d];
		  }
	      }
	  }
	  int s;cin>>s;
	  for(int n=0;n<s;n++){
	      int w;cin>>w;
	      cout<<a[b][w]<<endl;
	      }
      }
}

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

using namespace std;

typedef pair<int, int> P;

int main() {
	int m;
	while (cin >> m) {
		if (m == 0) {
			break;
		}
		vector<int> dp(7010, 0);
		vector<int> dp2(7010, 0);
		vector<P> data(m);
		for (int i = 0; i < m; i++) {
			cin >> data[i].first >> data[i].second;
		}
		for (int i = 0; i < m; i++) {
			vector<int> d;
			for (int j = 0; j < 7010; j++) {
				if (dp[j] > 0) {
					d.push_back(j);
				}
			}
			for (int j = 0; j < data[i].second; j++) {
				dp2[(data[i].first)*(j+1)]++;
			}
			for (int j = 0; j < d.size(); j++) {
				for (int k = 0; k < data[i].second; k++) {
					dp2[d[j]+(data[i].first)*(k+1)] += dp[d[j]];
				}
			}
			for (int j = 0; j < 7010; j++) {
				dp[j] = dp2[j];
			}
		}
		int g;
		cin >> g;
		int n;
		for (int i = 0; i < g; i++) {
			cin >> n;
			cout << dp[n] << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

int m;
int a[100], b[100];
int g, n;

int table[10010];

void print_table(){
	REP(i, 100){
		cout << table[i] << endl;
	}
}

void solve(){
	REP(i, m){
		for (int j = 10010 - 1; j >= 0; j--){
			if (table[j]){
				FOR(k, 1, b[i] + 1){
					table[j + a[i] * k] += table[j];
				}
			}
		}
	}
}


int main(){
	while (cin >> m&&m){
		fill(table, table + 10010, 0);
		table[0] = 1;
		REP(i, m){
			cin >> a[i] >> b[i];
		}
		solve();
		cin >> g;
		REP(i, g){
			cin >> n;
			cout << table[n] << endl;
		}

	}
}

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;
}