#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const ll INF = INT_MAX / 2;

// {a, t}
vector<pair<ll, int>> fs;

ll calc(ll x, const int N) {
    ll ans = x;
    for (int i = 0; i < N; i++) {
        const ll a = fs[i].first;
        const int t = fs[i].second;
        if (t == 1) {
            ans += a;
        } else if (t == 2) {
            ans = max(ans, a);
        } else {
            ans = min(ans, a);
        }
    }
    return ans;
}

int main(void) {
    int N;
    cin >> N;

    fs.resize(N);
    for (int i = 0; i < N; i++) {
        int a, t;
        cin >> a >> t;
        fs[i] = make_pair(a, t);
    }

    int Q;
    cin >> Q;

    const ll v_min = calc(-INF, N);
    const ll v_max = calc(INF, N);
#ifdef DEBUG
    printf("v_min: %lld, v_max: %lld\n", v_min, v_max);
#endif

    ll acc = 0;
    for (int i = 0; i < N; i++) {
        const ll a = fs[i].first;
        const int t = fs[i].second;
        if (t == 1) {
            acc += a;
        }
    }

    for (int i = 0; i < Q; i++) {
        ll x;
        cin >> x;
        if (x < v_min - acc) {
            printf("%lld\n", v_min);
        } else if (v_max - acc < x) {
            printf("%lld\n", v_max);
        } else {
            printf("%lld\n", x + acc);
        }
    }

    return 0;
}

#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

constexpr int N = 100005;

int n;
std::vector<long long> a[3];
std::string s;

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> n;
    for (int i = 1; i <= n << 1; ++i) {
        long long x;
        std::cin >> x >> s;
        if (s[0] == 'R') a[0].push_back(x);
        else if (s[0] == 'G') a[1].push_back(x);
        else a[2].push_back(x);
    }
    int x = -1, y = -1;
    for (int i = 0; i < 3; ++i) {
        if (a[i].size() & 1) {
            if (x != -1) y = i;
            else x = i;
        }
        std::sort(a[i].begin(), a[i].end());
    }
    if (x == -1) return std::cout << "0\n", 0;
    auto f = [](int x, int y) -> long long {
        long long ans = 1e18;
        for (long long i : a[x]) {
            long long t = std::lower_bound(a[y].begin(), a[y].end(), i) - a[y].begin();
            if (t != a[y].size()) ans = std::min(ans, std::abs(a[y][t] - i));
            if (t) ans = std::min(ans, std::abs(i - a[y][t - 1]));
        }
        return ans;
    };
    int z = 3 - x - y;
    long long ans = f(x, y);
    long long t1 = f(x, z), t2 = f(y, z);
    if (t1 < 1e18) ans = std::min(ans, t1 + t2);
    std::cout << ans << '\n';
    return 0;
}
