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 FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
inline double distance(double y1, double x1, double y2, double x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = { 0, 1, 0, -1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;


void solve(vector<pair<int, int>> v, vector<int> q) {
    VV<LL> dp(v.size() + 1, V<LL>(1001, 0));
    dp[0][0] = 1;

    FOR(i, 0, v.size()) {
        FOR(j, 0, dp[0].size()) {
            if (dp[i][j] == 0) { continue; }

            FOR(k, 0, v[i].second + 1) {
                int x = v[i].first * k;
                if (x < dp[0].size()) {
                    dp[i + 1][j + x] += dp[i][j];
                }
            }
        }
    }

    FOE(n, q) {
        print(dp[dp.size() - 1][n]);
    }
}

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

    while (true) {
        int M;
        cin >> M;
        if (M == 0) {
            break;
        }
        vector<pair<int, int>> v;
        FOR(_, 0, M) {
            int a, b;
            cin >> a >> b;
            v.emplace_back(make_pair(a, b));
        }

        vector<int> q;
        int G;
        cin >> G;
        FOR(_, 0, G) {
            int n;
            cin >> n;
            q.emplace_back(n);
        }

        solve(v, q);
    }
    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);}

/* }}} */

int m, card[7], num[7];
int n;
int dp[7001][8];

int dfs(int sum, int kind)
{
  if (dp[sum][kind] != -1) return (dp[sum][kind]);
  if (sum == n) return (1); // ok
  if (sum != n && kind == m) return (0); // ng

  int cnt = 0;
  REP(i, num[kind] + 1){
    cnt += dfs(sum + i * card[kind], kind + 1);
    // maisu 0 ~ num[kind]
    // sum + card[kind] * maisu <- next_sum
  }
  return (dp[sum][kind] = cnt);
}

signed main()
{
  ios::sync_with_stdio(false);
  
  while (cin >> m, m){
    REP(i, m) cin >> card[i] >> num[i];
    int g;
    cin >> g;
    REP(i, g){
      memset(dp, -1, sizeof(dp));
      cin >> n;
      print(dfs(0, 0));
    }
  }
  
  return (0);
}

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

int n,m;

void solve()
{
	int dp[11][15001] = {};
	int num,how;
	int res = 0;

	for(int i = 0; i < n; i++){
		cin >> num >> how;
		
		for(int j = 1; j <= how; j++){
			dp[i+1][num*j]++;
		}
		for(int j = 1; j <= 10000; j++){
			if(dp[i][j]){
				dp[i+1][j] += dp[i][j];
				for(int k = 1; k <= how && j+num*k < 10000; k++){
					dp[i+1][j+num*k] += dp[i][j];
				}
			}
		}
	}
	cin >> m;
	for(int i = 0; i < m; i++){
		cin >> num;
		cout << dp[n][num] << endl;
	}

	
	
}

int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}

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

using namespace std;

int saiki(vector<vector<pair<int,int> > > &cards,int ind,int m,int moku,int x){
    if(m > moku)return 0;
    if(ind==cards.size() && m==moku) return x;
    if(ind==cards.size()) return 0;

    int t = 0;
    for(int i=0;i<cards[ind].size();i++){
        t += saiki(cards,ind+1,m+cards[ind][i].first,moku,x*1);
    }
    return t;
}
int sub(vector<vector<pair<int,int> > >cards,int x){
    return saiki(cards,0,0,x,1);
}
int cnk(int n,int k){
    int x = 1;
    for(int i=0;i<k;i++) x *= (n-i);
    for(int i=0;i<k;i++) x /= (k-i);
    return x;
}
int solve(int m){
    vector<pair<int,int> > cards(m);
    for(int i=0;i<m;i++){
        int a,b;
        cin >> a >> b;
        cards[i] = make_pair(a,b);
    }
    vector<vector<pair<int,int> > > cards2(m);
    for(int i=0;i<m;i++){
        for(int j=0;j<cards[i].second+1;j++){
            cards2[i].push_back(make_pair(cards[i].first*j,cnk(cards[i].second,j)));
        }
    }
    int g;
    cin >> g;
    for(int i=0;i<g;i++){
        int x;
        cin >> x;
        cout << sub(cards2,x) << endl;
    }
}

int main(){
    while(true){
        int m;
        cin >> m;
        if(m==0)break;
        solve(m);
    }
    return 0;
}