#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
int main(void){
    ll n;cin>>n;
    ll a=1e18,b=-1e18,c=0;
    rep(i,n){
        ll x,t;cin>>x>>t;
        if(t==1){
            b+=x;
            a+=x;
            c+=x;
        }else if(t==2){
            chmax(b,x);
            chmax(a,x);
        }else{
            chmin(a,x);
            chmin(b,x);
        }
    }
    ll q;cin>>q;
    rep(i,q){
        ll x;cin>>x;
        ll res=min(max(c+x,b),a);
        cout<<res<<endl;
    }
}
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define pf push_front
#define F first
#define S second
#define MOD 1000000007
#define MAXN   1000001
#define MODA 998244353
#define all(a)      (a).begin(),(a).end()
#define allr(a)      (a).rbegin(),(a).rend()
const ll INF = numeric_limits<ll>::max() / 4;

ll power(ll a,ll b){
    ll result=1;
    while(b>0){
        int lastbit=(b&1);
        if(lastbit){
            result=(result*a);
        }
        a=(a*a);
        b=b>>1;
    }
    return result;
}

ll power2(ll a,ll b,ll p){
    ll result=1;
    while(b>0){
        int lastbit=(b&1);
        if(lastbit){
            result=(result*a)%p;
        }
        a=(a*a)%p;
        b=b>>1;
    }
    return result%p;
}

ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

vector<ll> Sieve(int n){
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++){
        if (prime[p] == true){
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    vector<ll>v;
    for (int p=2; p<=n; p++)
        if (prime[p]) v.pb(p);
    return v;
}

vector<ll> SeiveOfEratosthenes(int n){
    vector<ll>v(n+1,0);
    for(int i=0;i<=n;i++){
        v[i]=i;
    }

    for (int p=2; (p*p)<=n; p++){
        if (v[p] == p){
            v[p]=p;
            for (int i=p*p; i<=n; i += p) {
                v[i] = p;
            }
        }
    }
    return v;
}

vector<ll> SmallestPrimeFactors(int n){
    vector<ll>spf(n+1,0);
    spf[1] = 1;
    for (int i=2; i<=n; i++){
        spf[i] = i;
    }
    for (int i=4; i<=n; i+=2){
        spf[i] = 2;
    }
    for (int i=3; (i*i)<=n; i++){
        if (spf[i] == i){
            for (int j=i*i; j<MAXN; j+=i) {
                if (spf[j] == j){
                    spf[j] = i;
                }
            }
        }
    }
    return spf;
}

void shortest(map<int,vector<pair<int,ll>>>m,int src,int n,ll *dist){
    bool visited[n+1];
    set<pair<ll,int>>s;
    for(int i=0;i<=n;i++){
        dist[i]=-1;
        visited[i]=false;
    }

    s.insert(mp(0ll,src));
    dist[src]=0;
    while(!s.empty()){
        auto t=s.begin();
        ll dis=(*t).F;
        int u=(*t).S;
        s.erase(t);
        if(visited[u]){
            continue;
        }
        visited[u]=true;
        for(auto tt:m[u]){
            int v=tt.F;
            ll w=tt.S;
            if(!visited[v]){
                if(dist[v]!=-1 && dist[v]>dis+w){
                    s.erase(mp(dist[v],v));
                    dist[v]=dis+w;
                    s.insert(mp(dist[v],v));
                }else if(dist[v]==-1){
                    dist[v]=dis+w;
                    s.insert(mp(dist[v],v));
                }
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int ttttt=1;
//    cin>>ttttt;
    for(int tttt=1;tttt<=ttttt;tttt++){
        int n;
        cin>>n;
        ll a=0,b=-INF,c=INF;
        for(int i=0;i<n;i++){
            ll A,t;
            cin>>A>>t;
            if(t==1){
                a+=A;
                b+=A;
                c+=A;
            }else if(t==2){
                b=max(b,A);
                c=max(c,A);
            }else{
                c=min(A,c);
                b=min(A,b);
            }
        }
        ll q;
        cin>>q;
        while(q--){
            ll x;
            cin>>x;
            cout<<min(c,max(b,x+a))<<endl;
        }
        cout<<endl;
    }

    return 0;
}