#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<functional>
#include<climits>
using namespace std;
typedef pair<int,int>pii;
const int INF=1e9;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};

int a[7],b[7];
int cnt,to;
int n;
void dfs(int pos,int sum){
    if(pos==n){
        if(sum==to)cnt++;
        return;
    }
    for(int i=0;i<=b[pos];i++){
        dfs(pos+1,sum+a[pos]*i);
    }
}
int main(){
    while(scanf("%d",&n),n){
        for(int i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);
        int m;
        scanf("%d",&m);
        while(m--){
            scanf("%d",&to);
            cnt=0;
            dfs(0,0);
            printf("%d\n",cnt);
        }
    }
    return 0;
}