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

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

int rec(int p, int sum) {
  if(p == m) return sum == n;
  int cnt = 0;
  for(int i = 0; i <= b[p]; ++i)
    cnt += rec(p+1,sum+a[p]*i);
  return cnt;
}

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

C++: #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;

int cnt;
int m, num;
int cards[7][2];

void calc(int point, int sum) {
	for(int i=0; i<=cards[point][1]; i++) {
		int temp = sum;
		temp += cards[point][0]*i;
		if(temp == num) {
			cnt++;
			return;
		}
		else {
			if(point != m-1)
				calc(point+1, temp);
		}
	}
}

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

		for(int i=0; i<m; i++)
			cin >> cards[i][0] >> cards[i][1];

		int g;
		cin >> g;
		for(int i=0; i<g; i++) {
			cnt = 0;
			cin >> num;
			calc(0, 0);
			cout << cnt << endl;
		}
	}
	return 0;
}

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

int card_value[7];
int card_num[7];
int sum1[1001];
int sum2[1001];
int sum3[1001];

void get_sum(int a, int b, int sum[], int count){
	if(a >= b){
		if(count <= 1000){
			sum[count]++;
		}
		
	}else{
		for(int i = 0; i <= card_num[a]; i++){
			get_sum(a+1, b, sum, count + card_value[a] * i);
		}
	}
}
	

int main(){
	int g;
	int m;
	while(cin >> m){
		if(m == 0) break;
		for(int i = 0;i < m; i++){
			cin >> card_value[i] >> card_num[i];
		}
		for(int i = 0; i <= 1001; i++){
			sum1[i] = 0;
			sum2[i] = 0;
			sum3[i] = 0;
		}
		get_sum(0, m / 2, sum1, 0);
		get_sum(m / 2, m, sum2, 0);
		for(int i = 0; i < 1001; i++){
			for(int j = 0; j < 1001 - i; j++){
				sum3[i+j] = sum1[i]+sum2[j];
			}
		}
		cin >> g;
		for(int i = 0; i < g; i++){
			int q;
			cin >> q;
			cout << sum3[q] << endl;
		}
	}
	return 0;
}

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

using namespace std;

int main(void){

	int m,g,n,a[8],b[8],dp[9][1001];
	
	while(cin >> m,m){
		
		fill(a,a+8,0);
		fill(b,b+8,0);
		
		for(int i=1;i<=m;i++){
			cin >> a[i] >> b[i];
		}
		
		fill(dp[0],dp[9],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]>=0)dp[i+1][j]+=dp[i][j-k*a[i]];
				}
			}
		}

		cin >> g;
		while(g--){
			cin >> n;
			cout << dp[m+1][n] << endl;
		}
			
	}
	
return 0;
}

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;
    int dp[7+1][7000]={0};

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

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

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

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

int main() {
    ll m, g, a[10], b[10], dp[2005]{};
    while(cin >> m, m){
        for(int i=0;i<m;i++){
            cin >> a[i] >> b[i];
        }
        fill(dp, dp+1005, 0);
        dp[0] = 1;
        for(int j=0;j<m;j++){
            for(int i=1000;i>=0;i--){
                for(int k=1;k<=b[j];k++){
                    dp[i+a[j]*k] += dp[i];
                }
            }
        }
        cin >> g;
        for(int i=0;i<g;i++){
            int n;
            cin >> n;
            cout << dp[n] << endl;
        }
    }
    return 0;
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
vll dp(10000);
void mainmain(){
	int n;
	while(cin>>n,n){
		rep(i,10000) dp[i]=0;
		vector<pii> card;
		rep(i,n){
			int a,b;
			cin>>a>>b;
			card.PB(mkp(a,b));
		}
		dp[0]=1;
		// sort(ALL(card));
		rep(i,card.size()){
			vll tmp=dp;
			rep(k,card[i].S){
				int t=card[i].F+card[i].F*k;
				for(int j=10000;j>=t;j--){
					if(tmp[j-t]) dp[j]+=tmp[j-t];
				}
			}
			// rep(j,121){
				// cout<<j<<" "<<dp[j]<<endl;
			// }
			// cout<<endl;
		}
		// rep(i,200){
			// cout<<i<<" "<<dp[i]<<endl;
		// }
		int m;
		cin>>m;
		rep(i,m){
			int g;
			cin>>g;
			cout<<dp[g]<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}