#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
int main(){
    int N, Q;
    cin >> N;
    vector<long long> a(N), t(N);
    for(int i = 0; i < N; i++) cin >> a[i] >> t[i];
    long long MIN = -INF;//上限
    long long MAX = INF;//下限
    long long temp = 0;
    bool flag = false;
    long long f = -INF;
    for(int i = 0; i < N; i++){
        if(t[i] == 1) {
            temp += a[i];
            MAX += a[i];
            MIN += a[i];
            if(flag) f += a[i];
        }
        else if(t[i] == 2){
            MIN = max(MIN, a[i]);
            if(MIN >= MAX){
                flag = true;
                if(f == -INF) f = a[i];
            }
            if(flag) f = max(f, a[i]);
        }
        else{
            MAX = min(MAX, a[i]);
            if(MIN >= MAX) {
                flag = true;
                if(f == -INF) f = a[i];
            }
            if(flag)f = min(f, a[i]);
        }
    }
    long long mi = MIN, ma = MAX;
    if(!flag){
        for(int i = N - 1; i >= 0; i--){
            if(t[i] == 1){
                mi -= a[i];
                ma -= a[i];
            }
        }   
    }
    cin >> Q;
    for(int i = 0; i < Q; i++){
        long long x;
        cin >> x;
        if(flag) cout << f << endl;
        else{
            if(x > ma) cout << MAX << endl;
            else if(x < mi) cout << MIN << endl;
            else cout << x + temp << endl;
        }
    }
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
using namespace std;
using ll = long long;
#define INF 1001001001001001

int main (){
    ll n;
    cin >> n;

    ll max=-INF, min=INF, bias=0;
    rep(i, n){
        ll a; int t;
        cin >> a >> t;
        if(t==1){
            bias+=a;
            if(max>-INF) max += a;
            if(min<INF) min += a;
        }
        else if(t==2){
            if(a>max) max=a;
            if(a>min) min=a;
        }
        else if(t==3){
            if(a<min) min=a;
            if(a<max) max=a;
        }
    }
    ll q;
    cin >> q;
    rep(i, q){
        ll x;
        cin >> x;
        ll y = x+bias;
        if(y>=min) y=min;
        else if(y<=max) y=max;
        cout << y << endl;
    }

    return 0;
}