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<vector>
#include<map>
#define MAX 1001
using namespace std;

int M[MAX];
vector< vector<int> > v;

void compute( int depth, int s )
{
	if( depth == v.size() - 1 ){
		for(unsigned int i = 0; i < v[depth].size(); ++i){
			int t = s + v[depth][i];
			if( t < MAX ){
				M[ t ]++;
			}
		}
	}else{
		for(unsigned int i = 0; i < v[depth].size(); ++i){
			compute( depth + 1, s + v[depth][i] ); 
		}
	}
	return ;
}

int main()
{
	while( true ){
		int g, m;
		//vector< vector<int> > v;
		
		v.clear();
		for(int i = 0; i < MAX; ++i){
			M[i] = 0;
		}

		cin >> m;
		if( m == 0 )
			break;
		
		for(int i = 0; i < m; ++i){
			int a, b;
			cin >> a >> b;
			vector<int> vt;
			vt.push_back(0);
			for(int j = 1; j <= b; ++j){
				vt.push_back( a * j );
			}
			v.push_back( vt );
		}

		compute( 0, 0 );

		cin >> g;
		for(int x = 0; x < g; ++x){
			int n;
			cin >> n;

			cout << M[ n ] << endl;
		}
	}
	return 0;
}

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

using namespace std;

void solve()
{
	int m;
	while(cin >> m, m)
	{
		vector<int> a(m);
		vector<int> b(m);
		for(int i = 0; i < m; ++i)
		{
			cin >> a[i] >> b[i];
		}
		int dp[8][1001] = {0};
		dp[0][0] = 1;
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < 1001; ++j)
			{
				for(int k = 0; k <= b[i]; ++k)
				{
					if(j + k * a[i] > 1000)
					{
						break;
					}
					dp[i + 1][j + k * a[i]] += dp[i][j];
				}
			}
		}
		int g;
		cin >> g;
		for(int i = 0; i < g; ++i)
		{
			int declare;
			cin >> declare;
			cout << dp[m][declare] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<set>
#include<iostream>

using namespace std;

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

      int value[10001];
      for(int i=0;i<10001;i++)
	value[i] = 0;

      set<int> index;
      index.insert(0);
      value[0] = 1;

      for(int i=0;i<m;i++)
	{
	  int a,b;
	  cin >> a >> b;
	  set<int> next = index;
	  int next_vakue[10000];
	  for(int i=0;i<10001;i++)
	    next_vakue[i] = value[i];
	  for(set<int>::iterator it = index.begin();it != index.end();it++)
	    for(int j=1;j<=b;j++)
	      {
		if(next.find((*it)+j*a) == next.end())
		  next.insert((*it)+j*a);
		value[(*it)+j*a]+=next_vakue[(*it)];
	      }		

	  
	  index = next;
	}

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

    }
  return 0;
}

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

typedef pair <int, int> PII;
typedef vector <PII> VPII;
typedef vector <int> VI;

int dp[10][10000];

int main() {
    int m;
    while ( cin >> m && m ) {
        VPII v( m );
        for ( int i = 0; i < m; ++ i ) {
            cin >> v[i].first >> v[i].second;
        }
        for ( int i = 0; i < 10; ++ i ) {
            for ( int j = 0; j < 10000; ++ j ) {
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 1;
        for ( int i = 0; i < m; ++ i ) {
            for ( int j = 0; j < 10000; ++ j ) {
                for ( int k = 0; k <= v[i].second; ++ k ) {
                    int s = j + v[i].first * k;
                    if ( s >= 10000 ) continue;
                    dp[i+1][s] += dp[i][j];
                }
            }
        }
        int g;
        cin >> g;
        for ( int i = 0; i < g; ++ i ) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }
    return 0;
}

C++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 
                                                           
*/
/* }}} */

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

// #define int long long

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define print(x) cout << x << '\n'
#define Fi first
#define Se second

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = 1; i <= (n); i++)
#define DEPS(i, a, n) for (int i = (a); i >= n; i--)
#define DEP(i, n) for (int i = (n); i >= 0; i--)

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef pair<int, int> pii;

/* const */
const int INF = 999999999;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

/* func */
inline bool is_uruu(int y) {return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));}
inline bool inside(int y, int x, int H, int W) {return (y >= 0 && x >= 0 && y < H && x < W);}

/* }}} */

signed main()
{
  ios::sync_with_stdio(false);

  int m;
  while (cin >> m, m){
    int card[7], num[7];
    int dp[1001][8] = {1, 0};
    REP(i, m) cin >> card[i] >> num[i];

    REP(i, m){
      REP(j, 1001){
        REP(k, num[i] + 1){
          if (j + card[i] * k > 1000) continue;
          dp[j + card[i] * k][i + 1] += dp[j][i];
        }
      }
    }

    int g;
    cin >> g;
    REP(i, g){
      int n;
      cin >> n;
      print(dp[n][m]);
    }
  }

  return (0);
}