#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
    int N, Q; cin >> N;
    vector<int> A(N), T(N);
    REP(i, 0, N) cin >> A[i] >> T[i];

    cin >> Q;
    vector<ll> X(Q);
    REP(i, 0, Q) cin >> X[i]; 


    set<ll> S;
    REP(i, 0, Q) S.insert(X[i]);

    ll offset = 0;
    REP(i, 0, N) {
        if (T[i] == 1) {
            offset += A[i];
        } else if (T[i] == 2) {
            auto itr = S.lower_bound(A[i] - offset);

            bool deleted = false;
            auto jtr = S.begin();
            while (jtr != itr) {
                jtr = S.erase(jtr);
                deleted = true;
            }
            if (deleted) {
                S.insert(A[i] - offset);
            } 
        } else {
            auto itr = S.lower_bound(A[i] - offset);

            bool deleted = false;
            while (itr != S.end()) {
                itr = S.erase(itr);
                deleted = true;
            }
            if (deleted) {
                S.insert(A[i] - offset);
            } 
        }
    }
    // for (auto x : S) cout << x << " ";
    // cout << endl;

    REP(i, 0, Q) {
        auto itr = S.lower_bound(X[i]);
        if (itr != S.begin() && (itr == S.end() || *itr > X[i])) itr = prev(itr);
        // cout << "# i : " << i << ", X[i] : " << X[i] << ", itr : " << *itr << endl;
        cout << *itr + offset << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
typedef pair<int,int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e+9+7;


int main(){
    int n,k;
    ll ans = 0;
    cin >> n >> k;
    vector<vector<int>> t(n,vector<int>(n));
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> t[i][j];
        }
    }
    vector<int> perm(n-1);
    for(int i=0; i<n-1; i++){
        perm[i] = i;
    }
    do{
        int now = 0;
        int prev;
        ll sum = 0;
        for(int i=0; i<n-1; i++){
            prev = now;
            now = perm[i]+1;
            sum += t[prev][now];
        }
        sum += t[now][0];
        if(sum == k) ans++;
    }while(next_permutation(perm.begin(),perm.end()));
    cout << ans << endl;
    return 0;
}