/*/ 
   Author: Chaitanya_1999
   "Everything in this world is magic, except to the magician"
/*/
#include<bits/stdc++.h>
using namespace std;    

/*/---------------------------Defines-----------------------------------------/*/

#pragma GCC optimize("Ofast") 
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back 
#define eb emplace_back
#define fn for(int i =0 ;i <n;i++)  
#define fn1 for( int i =1;i<=n; i++)
#define fm for(int j =0 ;j <m;j++)
#define fm1 for(int j =1;j<=m;j++)
#define fi first
#define se second
#define endl '\n'
#define PI  3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
const int N = 2e6+5;    
const int INF = 1e18L;
// for all eight directions
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
// for all 4 directions
//int dx[4]={-1,1,0,0};
//int dy[4]={0,0,-1,1};
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*/-----------------------------Debug begins----------------------------------/*/
vector<string> split(const string& s, char c) {
  vector<string> v; stringstream ss(s); string x;
  while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
  stringstream s; s << "[";
  for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
  if(n>0)
  s << arr[n - 1] ;
  s<< "]";
  return s.str();
}
#define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);}
inline void __trace_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __trace_out_var(T val) { cerr << val; }
inline void __trace(vector<string>::iterator it) { cerr << endl; }
 
template<typename T, typename... Args>
inline void __trace(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __trace_out_var(a);
  cerr << "; ";
  __trace(++it, args...);
}
/*/-----------------------------Code begins----------------------------------/*/
int ar[N];
int dp[N];
signed main(){
  IOS;
  int T=1;
  // cin >> T;
  vector<int>tw;
  tw.pb(1);
  while(tw.back()<1e18L){
    tw.pb(tw.back()*2ll);
  }
  while(T--){
    int n;
    cin >> n ;
    int ans = INF;
    int k = 0;
    for(auto i : tw){
      int rem = n%i;
      int q = n/i;
      ans=min(ans,rem+q+k);
      k++;
    }
    assert(ans!=INF);
    cout << ans;

  }
  return 0;
}
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
using llong = long long;
using vi    = vector<int>;
using vvi   = vector<vi >;
using vvvi  = vector<vvi >;
using pii   = pair<int,int>;
constexpr int    INF  = 1e9;
constexpr llong  LINF = 1e18;
constexpr double EPS  = 1e-12;
constexpr int    MOD  = 1e9+7;
template<class Type>
void line(const Type &a){int cnt=0;for(const auto &elem:a)cerr<<(cnt++?' ':'>')<<elem;cerr<<endl;}

template<class Type> inline bool chmax(Type &a,const Type &b){if(a<b){a=b;return true;}return false;}
template<class Type> inline bool chmin(Type &a,const Type &b){if(a>b){a=b;return true;}return false;}

int main(){//I refer to other commentaries.
	int n;
	cin>>n;
	
	llong low=-LINF, high=LINF, add=0;
	REP(i,n){
		llong a;
		int t;
		cin>>a>>t;
		
		if(t==1){
			low+=a;
			high+=a;
			add+=a;
		}else if(t==2){
			chmax(low,a);
			chmax(high,a);
		}else{
			chmin(low,a);
			chmin(high,a);
		}
	}
	
	int q;
	cin>>q;
	while(q--){
		llong x;
		cin>>x;
		
		cout<<min(high,max(low,x+add))<<endl;
	}
}