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

int dp[1001];

int main(){
	int m, a, b, g, n;
	while(scanf("%d", &m), m){
		dp[0] = 1;
		for(int i = 1; i <= 1000; ++i){
			dp[i] = 0;
		}

		for(int i = 0; i < m; ++i){
			scanf("%d%d", &a, &b);
			for(int j = 1000; j >= a; --j){
				for(int k = 1; k <= b && j - k * a >= 0; ++k){
					dp[j] += dp[j - k * a];
				}
			}
		}

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

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 99999999



int main(){
    int n,g;
    while(cin>>n&&n){
        vector<pii> card;
        int sum=0;
        rep(i,n){
            int a,b;
            cin>>a>>b;
            card.pb(pii(a,b));
        }
        
        ll dp[8000]={};
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=8000-1;j>=1;j--){
                for(int k=1;k<=card[i-1].second;k++){
                    if(j-k*card[i-1].first>=0){
//                        if(dp[j-k*card[i-1].first]>0)cout<<i<<" "<<j<<" "<<k<<" "<<"dp["<<j<<"]="<<dp[j]<<"+"<<dp[j-k*card[i-1].first]<<" "<<dp[j]+dp[j-k*card[i-1].first]<<endl;
                        dp[j]+=dp[j-k*card[i-1].first];
                    }
                }
            }
        }

        cin>>g;
        rep(i,g){
            int m;
            cin>>m;
            cout<<dp[m]<<endl;
            
        }
    }
    
}

C++: #include <cstdio>
#include <utility>

using namespace std;

pair<int,int> card[8];
int m;
int dp[8][1005];
int g;

void debug(){
	const int mj = 30;
	for(int i=0;i<=m;i++){
		for(int j=0;j<mj;j++){
			printf("%2d ",dp[i][j]);
		}
		puts("");
	}
}

int main(){
	while(1){
		scanf("%d",&m);
		if(m==0){break;}
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d %d",&a,&b);
			card[i] = pair<int,int>(a,b);
		}
		for(int i=0;i<=m;i++){
			for(int j=0;j<=1000;j++){
				if(j == 0){ dp[i][j] = 1; continue;}
				if(i == 0 || j == 0){ dp[i][j] = 0; continue;}
				dp[i][j] = 0;
				dp[i][j] += dp[i-1][j];
				for(int k=1;k<=card[i].second;k++){
					if(j >= card[i].first * k){
						dp[i][j] += dp[i-1][j - card[i].first * k];
					}
				}
			}
		}
		//debug();
		scanf("%d",&g);
		for(int i=0;i<g;i++){
			scanf("%d",&a);
			//printf("dp[%d][%d]=",m,a);
			printf("%d\n",dp[m][a]);
		}
	}
}

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {
  int s, n;
};


int main() {
  int m;
  while(cin >> m) {
    if(!m) return 0;
    T card[7] = {};
    rep(i, 0, m) {
      int a, b;
      cin >> a >> b;
      card[i].s = a;
      card[i].n = b;
    }
    int pat[7001] = {};
    rep(i, 0, m) {
      dep(j, 7000, 0) {
        if(pat[j]) {
          rep(k, 1, card[i].n + 1) pat[j + card[i].s * k] += pat[j];
        }
      }
      rep(j, 1, card[i].n + 1) pat[card[i].s * j]++;
    }

    int g;
    cin >> g;
    rep(i, 0, g) {
      int n;
      cin >> n;
      cout << pat[n] << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int n;
int dp[ 8 ][ 1001 ];
int m, d, ans;
vector<PII> Cards;

int solve( const int now, const int now_score ) {
	if ( now == n ) {
		return now_score == 0;
	}
	if ( dp[ now ][ now_score ] >= 0 ) {
		return dp[ now ][ now_score ];
	}
	int res = 0;
	for ( int i = 0; i <= Cards[now].second; i++ ) {
		if ( now_score - Cards[ now ].first * i < 0 ) {
			break;
		}
		res += solve( now + 1, now_score - Cards[ now ].first * i );
	}
	return dp[ now ][ now_score ] = res;
}

int main() {
	while ( ~scanf( "%d", &n ) && n ) {
		NCLR( dp );
		Cards.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d %d", &Cards[ i ].first, &Cards[ i ].second );
		}
		sort( Cards.begin(), Cards.end() );
		scanf( "%d", &m );
		for ( int i = 0; i < m; i++ ) {
			scanf( "%d", &d );
			ans = solve( 0, d );
			printf( "%d\n", ans );
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 7
typedef pair<int,int> P;

int m, cnt;
vector<P> v;

void check(int num, int pos){
  if(num == 0){
    cnt++;
    return;
  }else if(num < 0 || m <= pos){
    return;
  }

  for(int i = 0 ; i < v[pos].second ; i++){
    check(num-v[pos].first*(i+1), pos+1);
  }
  check(num, pos+1);
}

int main(){
  int g, num;

  while(cin >> m ,m){
    v.clear(); v.resize(m);
    for(int i = 0 ; i < m ; i++){
      cin >> v[i].first >> v[i].second;
    }

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

  return 0;
}