#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ull> vull;
typedef pair<int, int> pii;

#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORDOWN(i, a, b) for (int i = a; i >= b; --i)
#define ff first
#define ss second
#define pb push_back

#define debug(x) cout << #x << " = " << x << '\n';

ull func(vector<ll> &v) {
    ull ans = 0;
    int n = v.size();
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j < n; ++j) {
            ans += abs(v[i] - v[j]);
        }
    }
    return ans;
}
/*
 * 3
10002 8668 92833
*/
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


    int n;
    cin >> n;

    vector<ll> v(n);
//    vector<ll> v1(n);

    for (int i = 0; i < n; ++i) {
        cin >> v[i];
//        v1[i] = v[i];
    }

    sort(v.rbegin(), v.rend());
    vector<ll> pref(n + 1);
    pref[0] = v[0];


    for (int i = 1; i < n; ++i) {
        pref[i] = pref[i - 1] + v[i];
    }


    ull ans = 0;
    for (ll i = 0; i < n - 1; ++i) {
        ans += abs(((ll)n - i - 1) * v[i] - (pref[n - 1] - pref[i]));
    }

    cout << ans;


    return 0;

}

// Standard I/O
#include <iostream>
#include <sstream>
#include <cstdio>
// Standard Library
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
// Template Class
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
// Container Control
#include <algorithm>

using namespace std;

#define rep( i, n ) for( int i = 0; i < n; ++i )
#define irep( i, n ) for( int i = n-1; i >= 0; --i )
#define reep( i, s, n ) for ( int i = s; i < n; ++i )
#define ireep( i, n, s ) for ( int i = n-1; i >= s; --i )
#define foreach(itr, x) for( typeof(x.begin()) itr = x.begin(); itr != x.end(); ++itr)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all( v ) v.begin(), v.end()
#define fs first
#define sc second
#define vc vector

// for visualizer.html
double SCALE = 1.0;
double OFFSET_X = 0.0;
double OFFSET_Y = 0.0;
#define LINE(x,y,a,b) cerr << "line(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(a) + OFFSET_X << ","										\
	<< SCALE*(b) + OFFSET_Y << ")" << endl;
#define CIRCLE(x,y,r) cerr << "circle(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(r) << ")" << endl;

typedef long long ll;
typedef complex<double> Point;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector< vector<int> > vii;
typedef vector< vector<double> > vdd;

typedef vector<int>::iterator vi_itr;

const int IINF = 1 << 28;
const ll IINFLL = 1ll << 61;
const int MOD = 1e9+7;
const double INF = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1.0);

// Direction : L U R D
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1 };

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

    ll c_a = 0, c_b = -IINFLL, c_c = IINFLL;
    rep(i, n) {
        ll a, t;
        cin >> a >> t;

        if ( t == 1 ) {
            c_a = c_a + a;
            c_b = a + c_b;
            c_c = a + c_c;
        } else if ( t == 2 ) {
            c_b = max(a, c_b);
            c_c = max(a, c_c);
        } else {
            c_b = min(a, c_b);
            c_c = min(a, c_c);
        }
    }
    
    int q;
    cin >> q;
    rep(i, q) {
        ll x;

        cin >> x;
        cout << min(max(x + c_a, c_b), c_c) << endl;
    }
}
