#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i=(a); i<(n); i++)
#define per(i, a, n) for (int i=(a); i>(n); i--)
typedef long long ll;
const int maxn = 2e5+5;
const int mod = 1e9+7;
using namespace std;

ll a[maxn], ti[maxn];

void solve() {
	ll n,q,t; cin >> n;
	ll L=0,R=0,add=0,ext=1e18,Lval=-1e18,Rval=1e18;
	rep(i,0,n) cin >> a[i] >> ti[i];
	rep(i,0,n) {
		if (ti[i] == 1) {
			if (ext != 1e18) {
				ext += a[i];
				continue;
			}
			add += a[i];
			if (Lval != -1e18) {
				Lval += a[i];
			}
			if (Rval != 1e18) {
				Rval += a[i];
			}
		} else if (ti[i] == 2) {
			if (ext != 1e18) {
				ext = max(ext, a[i]);
				continue;
			}
			if (a[i] < Lval) continue;
			L = a[i] - add;
			Lval = a[i];
			if (Lval >= Rval) {
				ext = Lval;
			}
		} else {
			if (ext != 1e18) {
				ext = min(ext, a[i]);
				continue;
			}
			if (a[i] > Rval) continue;
			R = a[i] - add;
			Rval = a[i];
			if (Rval <= Lval) {
				ext = Rval;
			}
		}
	}
	cin >> q;
	//cout << L << ' ' << Lval << ' ' << R <<  ' ' << Rval << '\n';
	rep(i,0,q) {
		cin >> t;
		if (ext != 1e18) cout << ext << '\n';
		else {
			if (Lval == -1e18 && Rval == 1e18) cout << t + add << '\n';
			else if (t <= L && Lval != -1e18) cout << Lval << '\n';
			else if (t >= R && Rval != 1e18) cout << Rval << '\n';
			else if (Rval != 1e18) cout << Rval - (R - t) << '\n'; 
			else cout << Lval + t - L << '\n';
		}
	}
}

int main(int argc, char * argv[]) {
	ios_base::sync_with_stdio(false); cin.tie(0);
	//int t; cin >> t; while(t--)
	solve();

    return 0;
}
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
#define all(v) (v).begin(),(v).end()
#define all_rev(v) (v).rbegin(),(v).rend()
#define debug(x) cout << #x << ": " << x << '\n';
#define degreeToRadian(deg) (((deg)/360)*2*M_PI)
#define radianTodegree(rad) (((rad)/2/M_PI)*360)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
using ll = long long;
using P = pair<int,int>;
using PL = pair<ll, ll>;
const ll INF = 1LL<<60;
const int MOD = 1e9 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  //cin.tie(0);ios::sync_with_stdio(false);
  //cout << fixed << setprecision(15);
  ll N;
  cin >> N;

  if (N < 1000) {
    cout << 0 << '\n';
    exit(0);
  }

  string t = "9999999999999999";
  ll ans = 0;
  
  while (stoll(t) >= N) t = t.substr(0, t.size() - 1);
  while (t.size() >= 3 && N > stoll(t)) {
    ans += (N - stoll(t)) * (t.size() / 3);

    N -= N - stoll(t);
    t = t.substr(0, t.size() - 1);
  }

  cout << ans << '\n';
  return 0;
}
