#include "bits/stdc++.h"
using namespace std;

int main() {
    ios::sync_with_stdio(false); 
    cin.tie(nullptr);

    int N; cin >> N;
    long long C_add = 0;
    long long C_min = 1e18;
    long long C_max = -1e18;
    for (int i = 0; i < N; i++) {
        long long a, t; cin >> a >> t;
        if (t == 1) {
            C_add += a;
            C_min += a;
            C_max += a;
        } else if (t == 2) {
            C_max = max(C_max, a);
            C_min = max(C_min, a);
        } else {
            C_min = min(C_min, a);
            C_max = min(C_max, a);
        }
    }

    int Q; cin >> Q;
    while (Q--) {
        long long x; cin >> x;
        cout << min(max(x + C_add, C_max), C_min) << '\n';
    }

    return 0;
}
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(time(0));
 
 
long long powm(long long a, long long b,long long mod) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a %mod;
        a = a * a %mod;
        b >>= 1;
    }
    return res;
}
 
 
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);      
}

int seg[6000055];

void upd(int ver,int tl,int tr,int pos,int val)
{
    if(tl>tr)
        return;
    else if(tl==tr)
        seg[ver]+=val;
    else
    {
        int tm=(tl+tr)/2;
        if(tm>=pos)
            upd(2*ver,tl,tm,pos,val);
        else
            upd(2*ver+1,tm+1,tr,pos,val);
        seg[ver]=min(seg[2*ver],seg[2*ver+1]);
    }
}

int get(int ver,int tl,int tr)
{
    if(tl>tr)
        return 0;
    else if(tl==tr)
        return tl;
    else
    {
        int tm=(tl+tr)/2;
        if(seg[2*ver]>0)
            return get(2*ver+1,tm+1,tr);
        else
            return get(2*ver,tl,tm);
    }
}


signed main()
{
 
ios_base::sync_with_stdio(false); 
cin.tie(NULL);  
cout.tie(0);   
 
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
        freopen("INPUT.txt","r",stdin);
        freopen("OUTPUT.txt","w",stdout);
    }
#endif          

    int n;
    cin>>n;
    int a[n],b[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
    }
    int mn=1e17;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j)
            {
                mn=min(mn,a[i]+b[j]);
            }
            else
                mn=min(mn,max(a[i],b[j]));
        }
    }
    cout<<mn;
}   