#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll N;
    cin >> N;
    vec a(N), t(N);
    REP(i,0,N) cin >> a[i] >> t[i];
    ll Q;
    cin >> Q;
    vec x(Q);
    REP(i,0,Q) cin >> x[i];

    ll l = -INF, u = INF, b = 0;;
    REP(i,0,N) {
        if(t[i] == 1) {
            if(l > -INF) l = MAX(l+a[i],-INF);
            if(u < INF) u = MIN(u+a[i],INF);
            b += a[i];
        }
        else if(t[i] == 2) {
            l = MAX(l,a[i]);
            if(l > u) u = l;
        }
        else {
            u = MIN(u,a[i]);
            if(l > u) l = u;
        }
    }

    REP(i,0,Q) {
        if(x[i] <= l - b) PR(l);
        else if(x[i] <= u - b) PR(x[i]+b);
        else PR(u);
    }

    return 0;
}

/*



*/
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e9;
int N, Q;

int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);
    ll mi = INF, mx = -INF, a = 0;
    cin >> N;
    for(int i=0;i<N;++i) {
        ll x, t; cin >> x >> t;
        if(t == 1) {
            mi += x;
            mx += x;
            a += x;
        }
        else if(t == 2) {
            mi = max(mi, x);
            mx = max(mx, x);
        }
        else {
            mi = min(mi, x);
            mx = min(mx, x);
        }
    }
    cin >> Q;
    for(int i=0;i<Q;++i) {
        ll x; cin >> x;
        cout << max(mx, min(mi, a+x)) << '\n';
    }
    return 0;
}