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

typedef pair<int,int> pii;
int table[16][1024] = {0};

int main() {
	int m;
	while(cin >> m, m) {
		vector<pii> v;

		for(int i = 0; i < 16; i++)
			for(int j = 0; j < 1024; j++)
				table[i][j] = 0;

		for(int i = 0; i < m; i++) {
			int a, b;
			cin >> a >> b;
			v.push_back(pii(a, b));
			//cout << a << " " << b << endl;
		}

		for(int i = 0; i <= v[0].second; i++)
			table[0][v[0].first * i] = 1;

		for(int i = 1; i < v.size(); i++) {
			for(int j = 0; j < 1024; j++) {
				for(int k = 0; k <= v[i].second; k++) {
					if(j - v[i].first * k >= 0) {
						table[i][j] += table[i - 1][j - v[i].first * k];
					}
				}
			}
		}

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

C++: /*
// 枝狩り無し全探索だと流石にきついか？ -> 普通に0.43secで通った
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<pair<int,int>> v;
int m, ans;

void dfs(int pos, int res){
    if(res == 0){
        ans++;
        return;
    }
    if(pos == m)    return;
    for(int j = min(v[pos].second, res/v[pos].first); j >= 0; j--){
        dfs(pos+1, res-j*v[pos].first);
    }
}

int main(){
    while(cin >> m, m){
        v.resize(m);
        for(int i = 0; i < m; i++)  cin >> v[i].first >> v[i].second;
        sort(v.rbegin(), v.rend());
        int g, n;
        cin >> g;
        while(g-- > 0){
            cin >> n;
            ans = 0;
            dfs(0, n);
            cout << ans << endl;
        }
    }
    return 0;
}
*/

// 恐らく想定解であるDP
#include<iostream>
using namespace std;

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

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int m;
vector<pii> card;
int memo[8192];
void search(int sum, int k)
{
	if(k == m)
		memo[sum]++;
	else{
		rep(i, card[k].second+1)
			search(sum + card[k].first * i, k+1);
	}
}

int main()
{
	while(scanf("%d", &m), m){
		card.resize(m);
		rep(i, m)
			scanf("%d%d", &card[i].first, &card[i].second);
		
		fill_n(memo, 8192, 0);
		search(0, 0);
		
		int g;
		scanf("%d", &g);
		rep(i, g){
			int n;
			scanf("%d", &n);
			printf("%d\n", memo[n]);
		}
	}
	
	return 0;
}

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

using namespace std;

struct P{
	int v;
	int n;
};

int func(P cd[7], int m, int n, int c, int p)
{
	if(c > n){
		return 0;
	}
	if(c == n){
		return 1;
	}
	int s=0;
	for(int i = p; i < m; i++){
		if(cd[i].n > 0){
			cd[i].n--;
			s += func(cd, m, n, c+cd[i].v, i);
			cd[i].n++;
		}
	}
	
	return s;
}

int main()
{
	int n, m, g;
	P cd[7];
	
	while(cin >> m && m){
		for(int i = 0; i < m; i++){
			cin >> cd[i].v >> cd[i].n;
		}
		cin >> g;
		for(int i = 0; i < g; i++){
			cin >> n;
			cout << func(cd, m, n, 0, 0) << endl;
		}
	}
	
	return 0;
}

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

using namespace std;

typedef pair<int,int> P;
const int MAX = 1001;

int Ans[8][MAX];
vector<P> V;

void make(){
  for(int i = 0; i < V.size(); i++)
    for(int k = 0; k < MAX; k++)
      if(Ans[k])
	for(int j = 0; j <= V[i].second; j++){
	  if(k+V[i].first*j >= MAX)break;
	  Ans[i+1][k+V[i].first*j] += Ans[i][k];
	}
}

void init(){
  memset(Ans,0,sizeof(Ans));
  Ans[0][0] = 1;
  V.clear();
}

int main(){

  int m;
  while(cin >> m && m){
    init();
    for(int i = 0; i < m; i++){
      P p;
      cin >> p.first >> p.second;
      V.push_back(p);
    }

    make();

    int g;
    cin >> g;
    while(g--){
      int n;
      cin >> n;
      cout << Ans[V.size()][n] << endl;
    }

  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  int n;
  while(R n && n) {
    P a[n];
    rep(i,n) cin >> a[i].F >> a[i].S;
    int d[1001];
    mem(d);
    d[0]=1;
    rep(i,n) {
      rrep(k,1000) {
        REP(j,1,a[i].S+1) {
          if(k+a[i].F*j<=1000) d[k+a[i].F*j]+=d[k];
        }
      }
    }
    int m;
    R m;
    while(m--) {
      int x;
      R x;
      pr(d[x]);
    }
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}

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

int main()
{
	int N,num[8],s[8];
	while(scanf("%d", &N), N)
	{
		for(int i=1; i<=N; i++)
			scanf("%d%d", &num[i], &s[i]);
		
		int dp[1001]={0};
		dp[0]=1;
		
		for(int i=1; i<=N; i++)
		{
			bool c[1001]={0};
			c[0]=1;
			for(int j=0; j<s[i]; j++)
			for(int k=1000; k>=0; k--)
			{
				if(k+num[i]>1000) continue;
				if(c[k]!=0) c[k+num[i]]=1;
			}
			
			for(int k=1000; k>=0; k--)
			for(int j=1; j<1000; j++)
			{
				if(k+j>1000) continue;
				if(c[j]!=0) dp[k+j]+=dp[k];
			}
		}
		
		int M;
		scanf("%d", &M);
		while(M--)
		{
			int t;
			scanf("%d", &t);
			printf("%d\n", dp[t]);
		}
	}
}