#include <iostream>
using namespace std;


int main(int argc, const char * argv[]) {
    
    while(true){
        int m;
        cin >> m;
        if(m == 0){
            break;
        }
        
        int a[7] = {0}, b[7] = {0};
        
        for(int i=0;i<m;i++){
            cin >> a[i] >> b[i];
        }
        
        int g;
        cin >> g;
        
        for(int i=0;i<g;i++){
            int n;
            cin >> n;
            int count[8] = {0};
            int win =0;
            while(true){
                count[0]++;
                
                for(int j=0;j<m;j++){
                    if(count[j] > b[j]){
                        count[j] = 0;
                        count[j+1]++;
                        continue;
                    }
                    else{
                        break;
                    }
                }
                
                if(count[m] > 0){
                    break;
                }
                
                int sum = 0;
                for(int j=0;j<m;j++){
                    sum += count[j] * a[j];
                }
                
                if(sum == n) win++;
                
            }
            
            cout << win << endl;
            
        }
        
        
    }
    
    return 0;
}