#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

using namespace std;

signed main()
{
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);

    string s; cin >> s;
    ll M; cin >> M;
    ll ma = -1;
    rep(i,s.size()) chmax(ma,s[i]-'0');

    if(s.size()==1){
        if((s[0]-'0')<=M) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }

    ll ok = ma,ng=1e18*2;

    while(ng-ok>1)
    {
        ll mid = (ok+ng)/2LL;
        vector<ll> v;
        ll m = M;
        while(m>0){
            v.pb(m % mid);
            m /= mid;
        }
        reverse(ALL(v));
        if(s.size()<v.size()) ok = mid;
        else if(s.size()>v.size()) ng = mid;
        else{
            rep(i,s.size()){
                if((ll)(s[i]-'0')<v[i]){ ok = mid; break;} 
                if((ll)(s[i]-'0')>v[i]){ ng = mid; break;}
            }
            if(ok!=mid&&ng!=mid) ok=mid;
        }
    }

    cout << ok-ma << endl;
    return 0;
}
// g++ main.cpp -o a.out && ./a.out

// 9223372036854775807
#include <bits/stdc++.h>
using namespace std;
int N, Q;
const int maxn = 2e5 + 5;
typedef long long ll;
inline ll read()
{
    ll x = 0;
    int s = 1;
    char c = getchar();
    while (c < 48 || c > 57)
    {
        if (c == '-')
            s = -1;
        c = getchar();
    }
    while (c >= 48 && c <= 57)
    {
        x = (x << 3) + (x << 1) + c - '0';
        c = getchar();
    }
    return x * s;
}
int main()
{
    int N, Q;
    N = read();
    ll a, t;
    ll l = -1e9, r = 1e9, b = 0;
    for (int i = 0; i < N; i++)
    {
        a = read();
        t = read();
        if (t == 1)
        {
            l += a;
            r += a;
            b += a;
        }
        else if (t == 2)
        {
            l = max(l, a);
            r = max(r, a);
        }
        else
        {
            l = min(l, a);
            r = min(r, a);
        }
    }
    Q = read();
    while (Q--)
    {
        ll x = read() + b;
        if (x < l)
            cout << l;
        else if (x > r)
            cout << r;
        else
            cout << x;
        cout << ' ';
    }
}