#include "bits/stdc++.h"
#include "random"
#include <unistd.h>

using namespace std;

#define ll long long
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vi>
#define pi  pair<int,int>
#define mp  make_pair
#define pb  push_back
#define MOD int(1e9) + 7
#define PAI  3.1415926535
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define chmax(x, y) x = max(x,y)
#define chmin(x, y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i, n) for(int i = 0 ; i < n; ++i)

const int dx2[4] = {0,1,0,1};
const int dy2[4] = {0,0,1,1};
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;

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

ll lcm(ll a,ll b){
  if(a < b)swap(a , b);
  return (a / gcd(a , b)) * b;
}

int Uniform_Random(int a, int b){ // (a <= x <= b)

  random_device rd;
  mt19937 mt(rd());
  
  uniform_int_distribution<int> rv1(a, b);
  return rv1(mt); 
}

int H, W;
vector<vector<bool>> used(16, vector<bool>(16, false));


int main(){
  
  int n; cin >> n;
  ll l = -linf, r = linf;
  ll s = 0;

  rep(i, n){
    ll a, t; cin >> a >> t;
    if(t == 1){
      s += a;
      l += a;
      r += a;
    }else if(t == 2){
      chmax(l, a);
      chmax(r, a);
    }else{
      chmin(l, a);
      chmin(r, a);
    }
  }

  int q; cin >> q;
  rep(i, q){
    ll x; cin >> x;
    ll ans = x + s;
    if(ans < l) ans = l;
    if(ans > r) ans = r;
    cout << ans << endl;
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> a(n),t(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> t[i];
  }
  int q;
  cin >> q;
  vector<int> x(q);
  for (auto &it:x) cin >> it;

  int sum = 0;
  int l = -INT_MAX;
  int r = INT_MAX;
  for (int i = 0; i < n; i++) {
    if (t[i] == 1) {
      sum += a[i];
      l += a[i];
      r += a[i];
    }
    if (t[i] == 2) {
      l = max(l, a[i]);
      r = max(r, a[i]);
    }
    if (t[i] == 3) {
      l = min(l, a[i]);
      r = min(r, a[i]);
    }
  }
  for (int i = 0; i < q; i++) {
    int res = x[i]+sum;
    res = max(l, res);
    res = min(r, res);
    cout << res << '\n';
  }
}
