#include "bits/stdc++.h"
using namespace std;

using ll = int64_t;

using vl = vector<ll>;

#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))

const ll INF = 1e18;

ll A, B, C, A2, B2, C2;
vl a(200000), t(200000);

void fix(ll& X, ll& Y, ll& Z, int idx) {
    if (t[idx] == 1) {
        X = a[idx], Y = -INF, Z = INF;
    }
    else if (t[idx] == 2) {
        X = 0, Y = a[idx], Z = INF;
    }
    else if (t[idx] == 3) {
        X = 0, Y = -INF, Z = a[idx];
    }
}

void solve() {
    int n; cin >> n;
    ice(i, 0, n) {
        cin >> a[i] >> t[i];
    }
    fix(A, B, C, 0);
    ice(i, 1, n) {
        fix(A2, B2, C2, i);
        ll X = A2 + A, Y = max(B2, min(A2 + C, A2 + B)), Z = min(C2, max(B2, A2 + C));
        A = X, B = Y, C = Z;
    }
    int q; cin >> q;
    ice(i, 0, q) {
        int x; cin >> x;
        cout << min(C, max(B, x + A)) << '\n';
    }
}

int main() {
    int tc = 1;
    ice(i, 0, tc) {
        solve();
    }
}
#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

long long N, A[1 << 18], T[1 << 18];
long long Q, X[1 << 18];

int main() {
	// Step #1. 入力
	cin >> N;
	for (int i = 0; i < N; i++) cin >> A[i] >> T[i];
	cin >> Q;
	for (int i = 0; i < Q; i++) cin >> X[i];

	// Step #2. 上限と下限を求める
	long long cl = -(1LL << 60), cr = (1LL << 60), sum = 0;
	for (int i = 0; i < N; i++) {
		if (T[i] == 1) { cl += A[i]; cr += A[i]; sum += A[i]; }
		if (T[i] == 2) { cl = max(cl, A[i]); cr = max(cr, A[i]); }
		if (T[i] == 3) { cl = min(cl, A[i]); cr = min(cr, A[i]); }
	}

	// Step #3. クエリに答える
	for (int i = 0; i < Q; i++) {
		long long val = 0;
		if (cl - sum <= X[i] && X[i] <= cr - sum) {
			val = X[i] + sum;
		}
		else if (X[i] <= cl - sum) {
			val = cl;
		}
		else if (cr - sum <= X[i]) {
			val = cr;
		}
		cout << val << endl;
	}
	return 0;
}