#include <sys/time.h>

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <utility>
#include <cctype>
#include <cstdlib>
#include <typeinfo>

/*
#include <atcoder/all>
using namespace atcoder;
*/

/*
#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;
using BigInt = mp::cpp_int;
*/

using namespace std;

using ll = long long;
using i_i = pair<int, int>;
using l_l = pair<ll, ll>;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)

const ll MOD = 1000000007LL;
const ll INF = 1e9 + 1;


//#define pos(x, y) (x) + (y) * w


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

	ll min_v = - 1e9;
	ll max_v = 1e9;
	ll total_a = 0;


	rep(i, n) {
		ll a, t;
		cin >> a >> t;
		if (t == 1) {
			total_a += a;
			max_v += a;
			min_v += a;
		} else if (t == 2) {
			min_v = max(a, min_v);
			max_v = max(a, max_v);
		} else if (t == 3) {

			min_v = min(a, min_v);
			max_v = min(a, max_v);
		}
	}
	//cout << "max_v:" << max_v << " min_v:" << min_v << " total_a:" << total_a << endl;
	int q;
	cin >> q;

	rep(i, q) {
		ll x;
		cin >> x;
		ll ans = x + total_a;
		ans = min(ans, max_v);
		ans = max(ans, min_v);
		cout << ans << endl;
	}



 



	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-8
// #define _
using namespace std;
typedef long long ll;
int main()
{
    #ifdef _
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif

    int n;
    ll ans=0;
    scanf("%d",&n);
    string s,t;
    while(s.size()<n+3) s+="110";
    cin>>t;
    for(int i=0;i<3;i++)
    {
        if(s.substr(i,n)==t)
        {
            ll l=i,r=3e10-n;
            ans+=ceil((r-l+1)/3.0);
        }
    }
    printf("%lld\n",ans);
    return 0;
}