#include<bits/stdc++.h>
typedef int64_t i64;
typedef long double f128;
using namespace std;

template<typename T>
void scan(T& n)
{
  cin>>n;
  return;
}

void scan()
{
  return;
}

template<typename T,class... Args>
void scan(T& n,Args&... args)
{
  scan(n);
  scan(args...);
  return;
}

template<typename T>
void scan_array(T start,T end)
{
  T now=start;
  for(;now!=end;++now)
  {
    scan(*now);
  }
  return;
}

template<typename T>
void print(T n)
{
  cout<<n;
  return;
}

template<typename T>
void println(T n)
{
  print(n);
  print('\n');
  return;
}

template<typename T,class... Args>
void println(T n,Args... args)
{
  print(n);
  print(' ');
  println(args...);
  return;
}

template<typename T>
void print_array(T start,T end)
{
  T now=start;
  print(*now);
  ++now;
  for(;now!=end;++now)
  {
    print(' ');
    print(*now);
  }
  print('\n');
  return;
}

i64 pow_mod(i64 a,i64 n,i64 mod)
{
  i64 res=1,now=a;
  while(n)
  {
    if(n&1)
    {
      res=res*now%mod;
    }
    now=now*now%mod;
    n>>=1;
  }
  return res;
}

i64 inv_mod(i64 a,i64 mod)
{
  return pow_mod(a,mod-2,mod);
}

i64 power(i64 a,i64 n)
{
  i64 res=1;
  for(i64 i=0;i<n;++i)
  {
    res*=a;
  }
  return res;
}

bool is_prime(i64 N)
{
  if(N<2)
  {
    return 0;
  }
  for(i64 i=2;i*i<=N;++i)
  {
    if(N%i==0)
    {
      return 0;
    }
  }
  return 1;
}

struct combination
{
  vector<i64> fact,inv,fact_inv;
  i64 mod=-1;
  combination(int Max,const i64 mod_):fact(Max+1,1),inv(Max+1,1),fact_inv(Max+1,1)
  {
    mod=mod_;
    for(i64 i=2;i<=Max;++i)
    {
      fact[i]=fact[i-1]*i%mod;
      inv[i]=mod-(mod/i*inv[mod%i]%mod);
      fact_inv[i]=fact_inv[i-1]*inv[i]%mod;
    }
  }
  i64 nCk(i64 n,i64 k)
  {
    if(n<k)
    {
      return 0;
    }
    return fact[n]*fact_inv[k]%mod*fact_inv[n-k]%mod;
  }
};

i64 comb(i64 n,i64 k)
{
  if(n<k)
  {
    return 0;
  }
  i64 res=1;
  for(i64 i=1;i<=k;++i)
  {
    res*=n-i+1;
    res/=i;
  }
  return res;
}

int main()
{
  string S;
  scan(S);
  reverse(S.begin(),S.end());
  for(size_t i=0;i<S.size();++i)
  {
    if(S[i]=='6')
    {
      S[i]='9';
      continue;
    }
    if(S[i]=='9')
    {
      S[i]='6';
    }
  }
  println(S);
  return 0;
}
#include <iostream>
 
using namespace std;
typedef long long ll;
const ll INF=2e18;
 
ll clamp(ll x, ll min, ll max) {
    if (x>max) {
        return max;
    }
    if (x<min) {
        return min;
    }
    return x;
}
 
int main() {
    ll n;
    cin>>n;
 
    ll low=-INF;
    ll high=INF;
    ll bias=0;
 
    for (ll i=1; i<=n; i++) {
        ll a,t;
        cin>>a>>t;
        if (1==t) {
            low+=a;
            high+=a;
            bias+=a;
        } else if (2==t) {
            low=max(low, a);
            high=max(high,a);
        } else {
            low=min(low, a);
            high=min(high, a);
        }
    }
 
    ll q;
    cin>>q;
    for (ll i=1; i<=q; i++) {
        ll x;
        cin>>x;
        cout<<clamp(x+bias, low, high)<<"\n";
    }
 
    return 0;
}