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>

using namespace std;

typedef long long ll;
typedef long double real;
typedef pair<int, int> duo;

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T In(){ T x; cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static inline ll pow_mod(ll a, ll k, ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
static inline ll mod_inv(ll a, ll p){ return pow_mod(a,p-2,p); }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
static const int Mod = 1000000000;

int main()
{
  int m;
  while (m = in()){
    int dp[2001] = {1};
    int next[2001] = {};
    vector<duo> cards;
    for (int i = 0; i < m; i++){
      int a, b;
      a = in();
      b = in();
      cards.push_back(duo(a, b));
    }
    for (int i = 0; i < cards.size(); i++){
      memcpy(next, dp, sizeof(next));
      for (int j = 1; j <= cards[i].second; j++){
        for (int k = 1000; k >= 0; k--){
          next[k + j * cards[i].first] += dp[k];
        }
      }
      memcpy(dp, next, sizeof(dp));
    }

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

  return (0);
}

// priority_queue queue deque first second front top push pop map set stringstream max_element min_element insert count find

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

int dp[1000+1];

void solve(int idx, int sum, const vector<pair<int, int> >& cards)
{
  if (sum > 1000)
    return;

  if (idx >= cards.size()) {
    ++dp[sum];
    return;
  }

  for (int i = 0; i <= cards[idx].second; ++i)
    solve(idx+1, sum+cards[idx].first*i, cards);
}

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

    vector<pair<int, int> > cards(m);
    for (int i = 0; i < m; ++i)
      cin >> cards[i].first >> cards[i].second;

    for (int i = 0; i <= 1000; ++i)
      dp[i] = 0;
    solve(0, 0, cards);

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

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

int main(){
  int m,g;
  int a[7],b[7];
  int n;
  int t[1001];
  int c[1001];

  while(1){
    cin >> m;
    if(!m)break;

    for(int i=0;i<m;i++)cin >> a[i] >> b[i];
    for(int i=1;i<=1000;i++){
      t[i] = 0;
      c[i] = 0;
    }
    t[0] = 1;
    c[0] = 1;
    
    for(int i=0;i<m;i++){
      for(int j=1;j<=b[i];j++){
	for(int k=0;k<=1000-a[i]*j;k++){
	  if(c[k]){
	    t[k+a[i]*j]+=c[k];
	  }
	}
      }
      for(int j=0;j<=1000;j++){
	if(t[j]>0)c[j] = t[j];
      }
    }
    
    cin >> g;
    for(int i=0;i<g;i++){
      cin >> n;
      cout << t[n] << endl;
    }
  }
}

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

int a[21];
int b[21];
int dp[21][2010]; // 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++ ) {
      int x, y;
      cin >> x >> y; a[i] = x, b[i] = y;
    }
    
    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 <cstdio>
#include <cstring>
typedef long long ll;
int m, g;
int a[7], b[7];
ll dp[8][1001];
int main() {
    while (scanf("%d",&m)) {
        if (!m) break;
        for (int i=0; i<m; i++) scanf("%d%d",&a[i],&b[i]);
        memset(dp, 0, sizeof(dp));
        dp[0][0]=1;
        for (int i=0; i<m; i++) {
            for (int k=0; k<=1000; k++) {
                for (int j=0; j<=b[i]; j++) {
                    if (k+a[i]*j>1000) continue;
                    dp[i+1][k+a[i]*j]+=dp[i][k];
                }
            }
        }
        scanf("%d",&g);
        for (int i=0; i<g; i++) {
            int n; scanf("%d",&n);
            printf("%lld\n",dp[m][n]);
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

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

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int dp[300][300][4];
int main(){
	int n;
	while(cin>>n,n){
		vi dp(10000);
		dp[0]=1;
		while(n--){
			int a,b;
			cin>>a>>b;
			for(int i=9999;i>=0;i--)if(dp[i]){
				loop(j,1,b+1)dp[i+j*a]+=dp[i];
			}
		}
		cin>>n;
		while(n--){
			int a;
			cin>>a;
			cout<<dp[a]<<endl;
		}
	}
}