#include <bits/stdc++.h>
using namespace std;
#define int long long
#define M 998244353
int binary_expo(int a,int b)
{
    int res=1;
    while(b>0)
    {
        if(b&1>0)
        {
            res=(((res%M)*(a%M))%M);
        }
        b=b>>1;
        a=(((a%M)*(a%M))%M);
    }
    return res;
}
int inv(int a,int b)
{
    a=(((a%M)*(binary_expo(b,M-2)))%M);
    return a;
}
int32_t main() 
{
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;cin>>n;
        vector<int> a(n,0);
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a.begin(),a.end());
        int p=0;
        int i=n-2;
        int s=0;
        while(i>=0)
        {
            s+=((a[i]*(binary_expo(2,p)))%M);
            s%=M;
            p++;
            i--;
        }
        int c=0;
        for(int i=n-1;i>0;i--)
        {
            int k=((s*a[i])%M);
            c+=k;
            c%=M;
            s=((s-a[i-1]+M)%M);
            s=(inv(s,(int)2)%M);
            s%=M;
        }
        for(int i=0;i<n;i++)
        {
            c+=((a[i]*a[i])%M);
            c%=M;
        }
        cout<<c<<endl;

    }
    return 0;
}

//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <atcoder/dsu>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
// using mint = modint;

using namespace std;

using ll = int64_t;
using ull = uint64_t;

[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)

template<typename T>
void chmin(T& x, T y) {
	x = min(x, y);
}

template<typename T>
void chmax(T& x, T y) {
	x = max(x, y);
}

ll csum(ll x, ll y) {
	if (x > 0 && y > 0 && x > LL_MAX - y) {
		return LL_MAX;
	}
	if (x < 0 && y < 0 && x < LL_MIN - y) {
		return LL_MIN;
	}
	return x + y;
}

ll readi() {
	ll ret;
	scanf("%" PRId64 "", &ret);
	return ret;
}

vector<ll> readvi(ll n) {
	vector<ll> ret(n);
	rep(i, 0, n) { ret[i] = readi(); }
	return ret;
}

double readf() {
	double ret;
	scanf("%lf", &ret);
	return ret;
}

string reads() {
	string s;
	cin >> s;
	return s;
}

void writei(ll x) {
	printf("%" PRId64 "\n", x);
}

void writevi(const vector<ll>& xs) {
    rep(i,0,xs.size()) {
        if (i < xs.size() - 1) {
            printf("%" PRId64 " ", xs[i]);
        } else {
            printf("%" PRId64 "\n", xs[i]);
        }
    }
}

void writes(const string& s) {
	cout << s.c_str() << endl;
}

void writef(double x) {
	printf("%.10f\n", x);
}

template<typename T>
vector<T> make_vec_nd(T init, ll size) {
	return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
	auto inner = make_vec_nd(init, rest...);
	return vector<decltype(inner)>(size, inner);
}

int main() {
	ll N = readi();
	vector<ll> As(N);
	vector<ll> Ts(N);
	ll zero_pt = 0;
	rep(i,0,N) {
		As[i] = readi();
		Ts[i] = readi();
		if (Ts[i] == 1) {
			zero_pt += As[i];
		} else if (Ts[i] == 2) {
			zero_pt = max(zero_pt, As[i]);
		} else {
			zero_pt = min(zero_pt, As[i]);
		}
	}
	ll total = 0;
	rep(i,0,N) {
		if (Ts[i] == 1) {
			total += As[i];
		} else {
			As[i] -= total;
		}
	}
	ll lower = LL_MIN;
	ll upper = LL_MAX;
	rep(i,0,N) {
		if (Ts[i] == 1) {
			continue;
		} else if (Ts[i] == 2) {
			chmax(lower, As[i]);
		} else {
			chmin(upper, As[i]);
		}
	}
	ll Q = readi();
	vector<ll> ans(Q);
	rep(i,0,Q) {
		ll x = readi();
		if (lower <= upper) {
			ans[i] = min(upper, max(lower, x)) + total;
		} else {
			ans[i] = zero_pt;
		}
	}
	rep(i,0,Q) {
		writei(ans[i]);
	}
}
