#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using T3 = tuple<int, int, int>;
template<typename T>void print(vector<T> a) { for (T t : a) { cout << t << " "; } cout << endl; }
template<typename T>void print2(T a, T b) { cout << a << " " << b << endl; }
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int mod = 1e9 + 7;

int main() {
    int n, q;
    cin >> n;

    ll s = 0;
    ll l = -LINF, r = LINF;
    rep(i, n) {
        ll a, t;
        cin >> a >> t;
        if (t == 1) {           // +
            s += a;
            l += a;
            r += a;
        } else if (t == 2) {    // max
            l = max(l, a);
            r = max(r, a);
        } else {                // min
            l = min(l, a);
            r = min(r, a);
        }
    }
    cin >> q;
    rep(iq, q) {
        ll x;
        cin >> x;
        ll ans = x + s;
        if (ans < l) { ans = l; }
        if (ans > r) { ans = r; }
        cout << ans << endl;
    }
    return 0;
}
#include "bits/stdc++.h"
using namespace std;
#define f                   first
#define s                   second
#define pb                  push_back
#define ll                  long long
#define ld                  long double
#define lb                  lower_bound
#define ub                  upper_bound
#define mp                  make_pair
#define mt                  make_tuple
#define pll                 pair<ll , ll>
#define vll                 vector<ll>
#define vvll                vector<vll>
#define vpll                vector<pll>
#define all(c)              (c).begin(),(c).end()
#define sz(c)               (int)(c.size())
#define get(x,c)            get<x>(c)
#define trav(a,x)           for(auto a = x.begin() ; a != x.end() ; a++)
#define rep(i, n)           for(int i = 0; i < (n) ; i++)
#define FOR(i, a, b)        for(int i = (a); i <= (b); i++)
#define FORR(i, b, a)       for(int i = (b); i >= (a); i--)

inline ll gcd(ll a,ll b) {if(b == 0) return a; a %= b; return gcd(b , a);}
inline ll max(ll a,ll b) {return((a > b) ? a : b);}
inline ll min(ll a,ll b) {return((a > b) ? b : a);}
ll power(ll x,ll ex);
ll powermod(ll x,ll ex,ll md);

const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;

const ll N = 1e5+11;


void solve(){
    ll n;
    cin >> n;

    vll a(n) , b(n) , c(n);
    rep(i , n) cin >> a[i];
    rep(i , n) cin >> b[i];

    rep(i , n) cin >> c[i];

    vll d(n);
    rep(i , n) d[i] = b[c[i] - 1];

    map<ll , ll> m1 , m2;

    rep(i , n){
        m1[a[i]]++;
        m2[d[i]]++;
    }

    ll ans = 0;
    trav(it , m1){
        ll x = it->f;

        auto it2 = m2.find(x);
        if(it2 != m2.end()){
            ans += (it->s)*(it2->s);
        }
    }
    cout << ans << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
   
    ll NTC=1;
    // cin>>NTC;

    #ifdef SIEVE
        sieve();
    #endif
    #ifdef NCR
        factorial();
    #endif 
    #ifdef PREPROCESS
        preprocess();
    #endif
    
    ll PTC=0;
    while((PTC++)<NTC){
        // cout << "Case #" << PTC << ":" << ' ';
        solve();
        //cout<<"\n";
    }
    //cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
}

ll power(ll x,ll y){if(y==0) return 1;ll a=power(x,y/2);if(y%2==0) return a*a;else return x*a*a;}
ll powermod(ll x,ll ex,ll md){ll ans=1ll;while(ex>0){if(ex&1ll) ans=(ans*x)%md; ex>>=1ll;x=(x*x)%md;}return ans;}
