import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

void readlnTo(T...)(ref T t) {
    auto s = readln().split();
    assert(s.length == t.length);
    foreach(ref ti; t) {
        ti = s[0].to!(typeof(ti));
        s = s[1..$];
    }
}

const real eps = 1e-10;

void main(){
    while(true) {
        auto m = readInt();
        if(m == 0) {
            return;
        }
        int[] a, b;
        foreach(i; iota(m)) {
            auto ab = readInts();
            a ~= ab[0];
            b ~= ab[1];
        }
        auto dp = new long[][](m+1, 1001);
        dp[0][0] = 1;
        foreach(i; iota(m)) {
            foreach(j; iota(1001)) {
                foreach(k; iota(b[i]+1)) {
                    if(j+k*a[i] > 1000) {
                        continue;
                    }
                    dp[i+1][j+k*a[i]] += dp[i][j];
                }
            }
        }
        auto g = readLong();
        foreach(i; iota(g)) {
            auto n = readInt();
            writeln(dp[m][n]);
        }
    }

}