#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=20000010;


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin>>n;
    vector<ll>a(n),t(n);
    rep(i,n)cin>>a[i]>>t[i];
    ll su=inf,ma=inf;
    ll de=-inf,mi=-inf;
    ll now=0;
    rep(i,n){
        if(t[i]==1){
            now+=a[i];
            ma+=a[i];
            mi+=a[i];
        }
        if(t[i]==2){
            if(mi>a[i])continue;
            if(ma<=a[i]){
                de=inf;
                su=-inf;
                ma=a[i],mi=a[i];
                continue;
            }
            de=a[i]-now;
            mi=a[i];
        }
        if(t[i]==3){
            if(ma<a[i])continue;
            if(mi>=a[i]){
                de=inf;
                su=-inf;
                ma=a[i],mi=a[i];
                continue;
            }
            su=a[i]-now;
            ma=a[i];
        }
        //cout<<de<<' '<<su<<' '<<mi<<' '<<ma<<endl;
    }
    ll q;cin>>q;
    rep(i,q){
        ll x;cin>>x;
        if(su<=de){
            cout<<ma<<endl;
        }else{
            if(x>=su){
                cout<<ma<<endl;
            }else if(x<=de){
                cout<<mi<<endl;
            }else{
                cout<<x+now<<endl;
            }
        }
    }
}


#include <bits/stdc++.h>
#include <fstream>
#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(3)<<(x)
#define all(v) (v).begin(), ( v).end()
using namespace  std;
#define forn(i, b) for (int i=0; i<b; ++i)
using ll = long long;
const int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int , int> pairs;
typedef complex<ll> G;
const int INF = 1e9 + 1 ;
const int N = 1e5 + 2;
using TP = tuple<int, int, int>;

int power(ll a,ll b){
    if(!b)
        return 1;
    int c=power(a,b/2);
    c=(1LL*c*c)%mod;
    if(b%2)
        c=(1LL*c*a)%mod;
    return c;
}
ll mul(ll A, ll B) {
	return (A*B)%mod;
}
struct Fenwick {

    vector<ll>ss;
    int n;
    void init(int n) {
        this->n = n;
        ss.resize(n + 1);
        for(int i = 0;i <= n;++i) {
            ss[i] = 0;
        }
    }
    ll get_val(int pos) {
        return ss[pos];
    }
    void add(int pos, ll x) {
        while(pos <= n) {
            ss[pos] += x;
            pos += (pos & -pos);
        }
    }
    void range_add(int l, int r, int val) {
        add(l, (ll)val);
        add(r + 1, -val);
    }
    ll sum(int pos) {
        ll ans = 0;
        while(pos >= 1) {
            ans += ss[pos];
            pos -= (pos & -pos);
        }
        return ans;
    }
    int point_query(int idx) {
    int ret = 0;
    for (idx; idx > 0; idx -= idx & -idx)
        ret += ss[idx];
    return ret;
    }
};



void done() {
    string s;
    cin >> s;
    int n = s.size();
    int ans = 0;
    for(int i = 0;i < 1 << n;++i) {
        int p = 0;
        int tot = 0;
        for(int ad  = 0; ad  < n;++ad) {
            if(i >> ad & 1) {
                ++p;
                tot += s[ad] - '0';
            }
        }
        if(tot % 3 == 0)ans = max(ans, p);
    }
    cout << (ans == 0 ? -1 : n - ans) << '\n';
}

struct Edge {
    int u, v, weight;
    bool operator<(Edge const &other) {
        return weight < other.weight;
    }
};



void solve()
{

    int n;
    cin >> n;
    vector<ll>a(n);
    for(int i = 0;i < n;++i)
        cin >> a[i];



}

void another() {



    ll n;
    cin >> n;
    int sz = to_string(n).size();
    int tot = 0;
    map<int, int>m;
    while(n) {
        m[(n%10)%3]++;
        tot += n % 10;
        n /= 10;
    }
    if(tot % 3 == 0) {
        cout << 0 << '\n';
        return;

    }
    int rem = tot % 3;
    if(sz == 1) {
        cout << -1 << '\n';
        return;
    }
    if(m[rem]) {
        cout << 1 << '\n';
        return;
    }
    if(rem == 2 && m[1] >= 2 && sz > 2) {
        cout << 2 << '\n';
        return;
    }
    cout << -1 << '\n';








}
void test_case() {
    int t;
    cin >> t;
    while(t--)another();
}
int main() {

    ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);

    done();

}
