#include <bits/stdc++.h>
using namespace std;
int t[200005];
long long a[200005];
long long sum[200005];

pair<long long, long long> tree[1000000];
void update(int n, int s, int e, int i, int t, long long v){
    if(s == e){
        if(t == 2) tree[n].first = v;
        else tree[n].second = v;
    }
    else{
        int mid = (s+e)/2;
        if(i <= mid) update(n+n, s, mid, i, t, v);
        else update(n+n+1, mid+1, e, i, t, v);
        tree[n].first = max(tree[n+n].first, tree[n+n+1].first);
        tree[n].second = min(tree[n+n].second, tree[n+n+1].second);
    }
}
pair<long long, long long> query(int n, int s, int e, int l, int r){
    if(s > r || l > e) return make_pair(-1e18, 1e18);
    if(l <= s && e <= r) return tree[n];
    int mid = (s+e)/2;

    pair<long long, long long> x = query(n+n, s, mid, l, r);
    pair<long long, long long> y = query(n+n+1, mid+1, e, l, r);
    return make_pair(max(x.first, y.first), min(x.second, y.second));
}

int n, q;
int find_max(long long sum, int i){
    int lo = i+1, hi = n, ans = -1;
    while(lo <= hi){
        int mid = (lo+hi)/2;
        if(query(1, 1, n, i+1, mid).first >= sum) ans = mid, hi = mid-1;
        else lo = mid+1;
    }
    return ans;
}
int find_min(long long sum, int i){
    int lo = i+1, hi = n, ans = -1;
    while(lo <= hi){
        int mid = (lo+hi)/2;
        if(query(1, 1, n, i+1, mid).second <= sum) ans = mid, hi = mid-1;
        else lo = mid+1;
    }
    return ans;
}

long long tmp[200005];
int main(){
    scanf("%d", &n);
    for(int i=1;i<=4*n;i++) tree[i] = make_pair(-1e18, 1e18);
    for(int i=1;i<=n;i++){
        scanf("%lld%d", &a[i], &t[i]);
        if(t[i] == 1) sum[i] = sum[i-1] + a[i];
        else sum[i] = sum[i-1];
        a[i] -= sum[i];
    }
    for(int i=n;i>=1;i--){
        if(t[i] == 2 || t[i] == 3){
            int mx = find_max(a[i], i);
            int mn = find_min(a[i], i);

            if(mx == -1 && mn == -1){
                tmp[i] = a[i] + sum[i] + (sum[n] - sum[i]);
            }
            else if(mx != -1 && mn == -1){
                tmp[i] = tmp[mx];
            }
            else if(mn != -1 && mx == -1){
                tmp[i] = tmp[mn];
            }
            else{
                tmp[i] = tmp[min(mn, mx)];
            }

            if(t[i] == 2){
                update(1, 1, n, i, 2, a[i]);
            }
            else{
                update(1, 1, n, i, 3, a[i]);   
            }
        }
    }

    scanf("%d", &q);
    for(int i=1;i<=q;i++){
        int x;
        scanf("%d", &x);
        int mx = find_max(x, 0);
        int mn = find_min(x, 0);

        long long ans = 0;
        if(mx == -1 && mn == -1){
            ans = x + sum[n];
        }
        else if(mx != -1 && mn == -1){
            ans = tmp[mx];
        }
        else if(mn != -1 && mx == -1){
            ans = tmp[mn];
        }
        else{
            ans = tmp[min(mn, mx)];
        }
        printf("%lld\n", ans);
    }
}
#include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cerr << #x << " = " << (x) << endl
#define show2(x,y) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) (find(all(v),x)!=v.end())
#define incls(s,c) (s.find(c)!=string::npos)
#define lb(a,x) distance((a).begin(),lower_bound(all(a),(x)))
#define ub(a,x) distance((a).begin(),upper_bound(all(a),(x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)),x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
//const int mod = 1000000007;
const double EPS = 1e-9;
//const long double EPS = 1e-14;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
inline int in() { int x; cin >> x; return x;}
inline ll lin() { ll x; cin >> x; return x;}
inline char chin() { char x; cin >> x; return x;}
inline string stin() { string x; cin >> x; return x;}
inline double din() { double x; cin >> x; return x;}
//template<class T = int> inline T in() { T x; cin >> x; return (x);}
template<typename T>inline ll suma(const vector<T>& a) { ll res(0); for (auto&& x : a) res += x; return res;}
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; }
char itoa(int n) { return n + '0';}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const int dy[4] = {1, 0, -1,  0};
const int dx[4] = {0, 1,  0, -1};

int d[20][20];
int dp[1<<17][17];

int main () {
	int n;
	cin >> n;
	vi x(n), y(n), z(n);
	rep(i,n) {
		cin >> x[i] >> y[i] >> z[i];
	}
	
	rep(i,n)rep(j,n) {
		if (i == j) continue;
		d[i][j] = INF;
	}
	rep(s,n)rep(t,n) {
		// s -> t
		int a = x[s];
		int b = y[s];
		int c = z[s];
		int p = x[t];
		int q = y[t];
		int r = z[t];
		
		int cost = abs(p-a) + abs(q-b) + max(0,r-c);
		d[s][t] = cost;
	}

	rep(i,1<<n)rep(j,n) dp[i][j] = INF;
	dp[(1<<n)-1][0] = 0;
	for (int S = (1<<n)-2; S >= 0; S--) {
		rep(v,n)rep(u,n) {
			if (!(S>>u&1)) {
				dp[S][v] = min(dp[S][v], dp[S|1<<u][u] + d[v][u]);
			}
		}
	}

	cout << dp[0][0] << endl;
	return 0;
}