#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define deb(x) cout << #x << " " << x << endl;
#define mod 1000000007
#define fast std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
const ll INF = 1e18;
const ll NEGINF = -1 * INF;

void solve()
{
    int n;
    cin >> n;
    ll x, t;
    ll a = 0;
    ll b = INT_MIN;
    ll c = INT_MAX;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> t;
        if (t == 1)
        {
            a += x;
            b += x;
            c += x;
        }
        else if (t == 2)
        {
            b = max(b, x);
            c = max(c, x);
        }
        else
        {
            b = min(b, x);
            c = min(c, x);
        }
    }
    // deb(a);
    // deb(b);
    // deb(c);
    int q;
    cin >> q;
    while (q--)
    {
        cin >> x;
        cout << min(c, max(b, a + x)) << endl;
    }
}

int main()
{
    fast;
#ifndef ONLINE_JUDGE
    freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/input.txt", "r", stdin);
    freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/output.txt", "w", stdout);
#endif
    int T = 1;
    while (T--)
    {
        solve();
    }
    //cout<< "Done in " << clock() / CLOCKS_PER_SEC <<"sec"<< endl;
    return 0;
}
//BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define all(v) v.begin(),v.end()
#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> pii;
typedef pair<ll, ll> LL;
typedef vector<ii> vii;
typedef priority_queue<int,vector<int>,greater<int> > PQ;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int setBit(int mask, int pos){return mask = mask | (1<<pos);}
bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));}

int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
int arr[60];
set<int> s[60];
int vis[60];

void solve()
{
    int n;

    cin>>n;

    REP(i, n) {
        cin>>arr[i];
        for(int j = 0; j < 15; j++) {
            if(arr[i] % primes[j] == 0) {
                while(arr[i] % primes[j] == 0) {
                    arr[i] /= primes[j];
                }
                s[i].insert(primes[j]);
            }
        }
        if(arr[i] != 1) {
            s[i].insert(arr[i]);
        }
    }

    ll ans = 1e18;

    for(int i = 0; i < (1<<15); i++) {
        ll res = 1;
        for(int j = 0; j < n; j++)
            vis[j] = 0;
        for(int j = 0; j < 15; j++) {
            if(checkBit(i, j)) {
                res = (1LL * res * primes[j]);
                for(int k = 0; k < n; k++) {
                    if(!vis[k]) {
                        if(s[k].count(primes[j])) {
                            vis[k] = 1;
                            continue;
                        }
                    }
                }
            }
        }
        bool flag = true;
        for(int j = 0; j < n; j++) {
            if(!vis[j]) {
                flag = false;
                break;
            }
        }

        if(flag) {
            ans = min(ans, res);
        }
    }

    cout<<ans<<endl;

    return;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int test = 1;

    //cin>>test;

    while(test--) {
        solve();
    }

    return 0;
}

