#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
using ll = long long;
const ll INF = 1e9;

int n,q;
vector<ll> a,t,x;

void solve() {
  ll s = 0, l = -INF, r = INF;
  REP(i,n) {
    if (t[i] == 1) { // +
      s += a[i];
      l += a[i];
      r += a[i];
    } else if (t[i] == 2) { // max
      l = max(l,a[i]);
      r = max(r,a[i]);
    } else { // min
      l = min(l,a[i]);
      r = min(r,a[i]);
    }
  }

  REP(i,q) {
    ll ans = x[i]+s;
    if (ans < l) ans = l;
    if (r < ans) ans = r;
    cout << ans << endl;
  }
  return;
}

int main() {
  cin >> n;
  a.resize(n);
  t.resize(n);
  REP(i,n) cin >> a[i] >> t[i];

  cin >> q;
  x.resize(q);
  REP(i,q) cin >> x[i];

  solve();
  return 0;
}
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long int
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define pci pair<char,int
#define mii map<int,int>
#define mll map<long long int,long long int>
#define mci map<char,int>
#define umii unordered_map<int,int>
#define umll unordered_map<long long int,long long int>
#define umci unordered_map<char,int>
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define mod 998244353
using namespace std;

bool isPrime(ll n) 
{ 
    // Corner cases 
    if (n <= 1) 
        return false; 
    if (n <= 3) 
        return true; 
  
    // This is checked so that we can skip 
    // middle five numbers in below loop 
    if (n % 2 == 0 || n % 3 == 0) 
        return false; 
  
    for (ll i = 5; i * i <= n; i = i + 6) 
        if (n % i == 0 || n % (i + 2) == 0) 
            return false; 
  
    return true; 
} 
ll power(ll x,ll n)
{ll x1=0;
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x);
       //  if(result>=1000000001) return 0;
        x=(x*x);
      //   if(x>=1000000001) return 0;
        n=n/2;
    }
    return result;
}
ll countDivisors(ll n) 
{ 
    ll cnt = 0; 
    for (ll i = 1; i <= sqrt(n); i++) { 
        if (n % i == 0) { 
        
    
            if (n / i == i) 
                cnt++; 
  
            else 
                cnt = cnt + 2; 
        } 
    } 
    return cnt; 
} 
int main(){
   IOS;
ll t;t=1;

while(t--){
ll n;cin>>n;
while(n%2==0){
    n=n/2;
}
cout<<2*(countDivisors(n));

 
} 
    
    

    
return 0;    
}