#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

int main() {
	int N;
	cin >> N;
	vector<int> A(N), T(N);
	REP(i, N) scanf("%d %d", &A[i], &T[i]);
	
	ll ub = longINF, lb = -longINF, s = 0;
	for (int i = N - 1; i >= 0; --i) {
		if (T[i] == 1) {
			ub -= A[i];
			lb -= A[i];
			s += A[i];
		}
		else if (T[i] == 2) chmax(lb, A[i]);
		else chmin(ub, A[i]);
	}
	
	auto func = [&](ll st) {
		ll res = st;
		REP(i, N) {
			if (T[i] == 1) res += A[i];
			else if (T[i] == 2) chmax(res, A[i]);
			else chmin(res, A[i]);
		}
		return res;
	};
	
	ll ubv = func(ub);// cout << ub << ", " << ubv << endl;
	ll lbv = func(lb);// cout << lb << ", " << lbv << endl;
	
	// cout << "lb = " << lb << ", ub = " << ub << endl;
	// cout << s << endl;
	
	int Q;
	cin >> Q;
	while (Q--) {
		int x;
		scanf("%d", &x);
		if (lb < x && x < ub) printf("%lld\n", x + s);
		else if (x >= ub) printf("%lld\n", ubv);
		else printf("%lld\n", lbv);
	}
}
/*#pragma GCC optimize("Ofast")
 #pragma GCC optimize ("unroll-loops")
 #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 #pragma comment(linker, "/stack:200000000")*/
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 998244353
#define MAXN  21
#define all(v) v.begin(),v.end()
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
 tree_order_statistics_node_update>
 ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0||y < 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0||r==n)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n;
    cin>>n;
    int a[n+1];
    int ans=0;
    map<int,int> m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans+=i-1-m[a[i]];
        m[a[i]]++;
    }
    cout<<ans<<endl;
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
        
    }
}
