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 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<cstdio>

using namespace std;

int main()
{
	for(int m;scanf("%d",&m),m;){
		int cnt[1001];
		for(int i=0;i<=1000;i++)	cnt[i]=(i==0);

		for(int i=0;i<m;i++){
			int a,b;	scanf("%d%d",&a,&b);
			for(int j=1000;j>=0;j--){
				if(cnt[j]==0)	continue;
				for(int k=1;k<=b;k++){
					if(j+a*k>1000)	break;
					cnt[j+a*k]+=cnt[j];
				}
			}
		}

		int g;	scanf("%d",&g);
		for(int i=0;i<g;i++){
			int n;	scanf("%d",&n);
			printf("%d\n",cnt[n]);
		}
	}

	return 0;
}

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

int a[10002];
int b[10002];
int dp[10002][10002]; // 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;
int main(){
  int m,a[10],b[10],g,n;
  
  while(cin >> m,m!=0){
    for(int i=1;i<=m;i++){
      cin >> a[i] >> b[i];
    }
    
    int sum,dp[10][1010]={};
    for(int i=1;i<=m;i++){
      sum = 0;
      dp[i-1][0] = 1;
      for(int j=1;j<=1000;j++){
	dp[i][j] = dp[i-1][j];
      }
      for(int j=0;j<b[i];j++){
	sum += a[i];
	for(int k=sum;k<=1000;k++){
	  dp[i][k] += dp[i-1][k-sum];
	}
      }
    }
    
    cin >> g;
    for(int i=0;i<g;i++){
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }
  
  return(0);
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
	int dp[8][1001];
	int i,j,n,m,a[8],b[8],g,suu[11],k,x,maisu;
	while(1) {
		scanf("%d",&m);
		if(m==0) break;
		for(i=0;i<m;i++) scanf("%d %d",&a[i],&b[i]);
		scanf("%d",&g);
		for(i=0;i<g;i++) scanf("%d",&suu[i]);
		for(i=0;i<=m;i++) {
			for(j=0;j<=1000;j++) {
				dp[i][j]=0;
			}
		}
		dp[0][0]=1;
		for(i=0;i<m;i++) {
			maisu=b[i];
			x=a[i];
			for(j=0;j<=1000;j++) {
				for(k=0;k<=maisu;k++) {
					if(j+x*k<=1000) {
						dp[i+1][j+x*k]+=dp[i][j];
					}
				}
			}
		}
//		for(i=0;i<=m;i++) {
//			for(j=0;j<=168;j++) {
//				printf("%8d",dp[i][j]);
//			}
//			printf("\n");
//		}
		for(i=0;i<g;i++) printf("%d\n",dp[m][suu[i]]);
	}
	return 0;
}

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

int main(void)
{
  while (1) {
    int g, m;
    int table[1001] = {};
    table[0] = 1;

    cin >> m; if (!m) break;
    for (int i=0; i<m; i++) {
      int a, b; cin >> a >> b;
      for (int j=1000; j>=0; j--) {
	if (!table[j]) continue;
	for (int k=1; k<=b; k++) {
	  if (j+a*k > 1000) break;
	  table[j+a*k] += table[j];
	}
      }
    }

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

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define fi first
#define sec second
#define pb push_back
int num[10],cnt[10];
int sum[10000];
int m;
void dfs(int x,int s){
	if(x==m){
		sum[s]++;
		return;
	}
	for(int i=0;i<=cnt[x];i++){
		dfs(x+1,s+num[x]*i);
	}
}
int main(){
	while(1){
		memset(sum,0,sizeof(sum));
		scanf("%d",&m);
		if(m==0)break;
		for(int i=0;i<m;i++){
			scanf("%d %d",&num[i],&cnt[i]);
		}
		dfs(0,0);
		int n,g;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&g);
			printf("%d\n",sum[g]);
		}
	}
	return 0;
}

C++: // AOJ 0154
#include<iostream>
#include<cstring>

int m;
int a[7], b[7];
int dp[8][1001];

int main()
{
	while( std::cin >> m, m )
	{
		memset( dp, 0, sizeof( dp ) );

		for( int i = 0; i != m; ++i )
			std::cin >> a[i] >> b[i];

		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]; ++k )
					if( j + k * a[i] <= 1000 )
						dp[i+1][j+k*a[i]] += dp[i][j];

		int g;
		std::cin >> g;
		while( g-- )
		{
			int n;
			std::cin >> n;
			std::cout << dp[m][n] << std::endl;
		}
	}

	return 0;
}

C++: /*
 * 0154.cpp
 *
 *  Created on: 2011/07/27
 *      Author: isa
 */

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int main(void){
	int m,g,tmp;

	while(cin >> m && m){
		int c[m],n[m],dp[8][1001];

		for(int i=0;i<m;i++){
			cin >> c[i] >> n[i];
		}
		memset(dp,0,sizeof(dp));
		dp[0][0] = 1;
		for(int i=0;i<m;i++){
			for(int j=0;j<=1000;j++){
				for(int k=0;k<=n[i];k++){
					if(j+k*c[i] > 1000) break;
					dp[i+1][j+k*c[i]] += dp[i][j];
				}
			}
		}
		cin >> g;
		for(int i=0;i<g;i++){
			cin >> tmp;
			cout << dp[m][tmp] << endl;
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}

#define MAX 7*100*10+10

signed main(void) {
    int n;
    while(cin >> n, n){
        int dp[10][MAX] = {};
        dp[0][0] = 1;
        rep(i,n){
            int a,b;
            cin >> a >> b;
            rep(j,MAX)if(dp[i][j] > 0){
                rep(k,b+1){
                    dp[i+1][j+a*k] += dp[i][j];
                }
            }
        }

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