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<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int m, a[10], b[10], n, g, ans[1010];
void f(int sum, int i) {
	if (sum > 1000)return;
	if (i == m) {
		ans[sum]++; return;
	}
	for0(p, b[i] + 1) f(sum + a[i] * p, i + 1);
}
int get() { int r; cin >> r; return r; }
signed main() {
	while (cin >> m, m) {
		for0(i, m)cin >> a[i] >> b[i];
		for0(i, 1001)ans[i] = 0;
		f(0, 0);
		cin >> g;
		while (g--)cout << ans[get()] << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int solve( VI as, VI bs, int n )
{
	const int m = as.size();

	VVI dp( m + 1, VI( m * 100 * 10 + 1, 0 ) );
	dp[0][0] = 1;

	REP( i, 0, m )
	{
		REP( j, 0, dp[i].size() )
		{
			REP( k, 0, bs[i] + 1 )
			{
				if ( !dp[i][j] )
				{
					continue;
				}

				dp[ i + 1 ][ j + as[i] * k ] += dp[i][j];
			}
		}
	}

	return dp[m][n];
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int m;
		cin >> m;

		if ( !m )
		{
			break;
		}

		VI as( m ), bs( m );

		REP( i, 0, m )
		{
			cin >> as[i] >> bs[i];
		}

		int g;
		cin >> g;

		REP( i, 0, g )
		{
			int n;
			cin >> n;

			cout << solve( as, bs, n ) << endl;
		}
	}

	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[],int b[],int c[],int d[],int p,int q,int m,int n){
  int sm=0;
  if(p!=m){
    if(d[p]+q>=n){
      sm+=f(a,b,c,d,p+1,q,m,n);
      for(c[p]++;c[p]<b[p]+1;c[p]++){
	q+=a[p];
	if(0){
	}else if(q>n){
	  break;
	}else if(q<n){
	  sm+=f(a,b,c,d,p+1,q,m,n);
	}else{
	  sm++;
	  break;
	}
      }
      c[p]=0;
    }
  }
  return sm;
}
int main(){
  int i;
  int a[7],b[7],c[7],d[8],z[10],m,n,o;
  memset(c,0,sizeof(c));
  while(cin>>m&&m){
    for(i=0;i<m;i++)
      cin>>a[i]>>b[i];
    d[m]=0;
    for(i=m-1;i>-1;i--)
      d[i]=d[i+1]+a[i]*b[i];
    cin>>o;
    for(i=0;i<o;i++){
      cin>>n;
      z[i]=f(a,b,c,d,0,0,m,n);
    }
    for(i=0;i<o;i++)
      cout<<z[i]<<endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

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

static const double eps = 10e-6;

int amount[101];
int num[101];
int dp[101][1001];

int main(){
	string str;
	vector<string> data;
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;
		memset(amount,0,sizeof(amount));
		memset(num,0,sizeof(num));
		memset(dp,0,sizeof(dp));

		for(int i=0;i<n;i++){
			int a,b;
			scanf("%d %d",&a,&b);
			amount[i] = b;
			num[i] = a;
		}

		dp[0][0] = 1;


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

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

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0154 : Sum of Cards **/
int main()
{
	int N;
	while (cin>>N, N) {
		int c[7], num[7];
		rep(i, N) {
			cin>>c[i]>>num[i];
		}
		int dp[8][1001];
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		
		for (int i=0; i<N; i++) {
			for (int j=0; j<=1000; j++) {
				for (int k=0; k<=num[i]; k++) {
					if (j+k*c[i]<=1000) {
						dp[i+1][j+k*c[i]] += dp[i][j];
					}
				}
			}
		}
		
		
		int G;
		cin>>G;
		rep(i, G) {
			int t; cin>>t;
			cout << dp[N][t] << endl;
		}
	}
}

C++: #include <stdio.h>
#include <string.h>

int main(void){
  int i,j,k;
  int n,m,val,a[7],b[7];
  int dp[8][1001];

  while(scanf("%d",&n), n){
    for(i=0;i<n;i++){
      scanf("%d%d",&a[i],&b[i]);
    }

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

    //J[hÌÔ
    for(i=0;i<n;i++){
      //v_
      for(j=0;j<=1000;j++){
        //iÔÚÌJ[hð½gp·é©
        for(k=0;k<=b[i];k++){
          if(j+a[i]*k>1000) continue; //1001ÈãÍ¢çÈ¢ÌÅ³
          dp[i+1][j+a[i]*k] += dp[i][j];
        }
      }
    }

    scanf("%d",&m);
    while(m--){
      scanf("%d",&val);
      printf("%d\n",dp[n][val]);
    }
  }

  return 0;
}