Problem:
Score : 1300 points

Problem Statement

A museum exhibits N jewels, Jewel 1, 2, ..., N.
The coordinates of Jewel i are (x_i, y_i) (the museum can be regarded as a two-dimensional plane), and the value of that jewel is v_i.

Snuke the thief will steal some of these jewels.

There are M conditions, Condition 1, 2, ..., M, that must be met when stealing jewels, or he will be caught by the detective.
Each condition has one of the following four forms:

(t_i =L, a_i, b_i) : Snuke can only steal at most b_i jewels whose x coordinates are a_i or smaller.

(t_i =R, a_i, b_i) : Snuke can only steal at most b_i jewels whose x coordinates are a_i or larger.

(t_i =D, a_i, b_i) : Snuke can only steal at most b_i jewels whose y coordinates are a_i or smaller.

(t_i =U, a_i, b_i) : Snuke can only steal at most b_i jewels whose y coordinates are a_i or larger.

Find the maximum sum of values of jewels that Snuke the thief can steal.

Constraints

1 \leq N \leq 80

1 \leq x_i, y_i \leq 100

1 \leq v_i \leq 10^{15}

1 \leq M \leq 320

t_i is L, R, U or D.

1 \leq a_i \leq 100

0 \leq b_i \leq N - 1

(x_i, y_i) are pairwise distinct.

(t_i, a_i) are pairwise distinct.

(t_i, b_i) are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 v_1
x_2 y_2 v_2
:
x_N y_N v_N
M
t_1 a_1 b_1
t_2 a_2 b_2
:
t_M a_M b_M

Output

Print the maximum sum of values of jewels that Snuke the thief can steal.

Sample Input 1

7
1 3 6
1 5 9
3 1 8
4 3 8
6 2 9
5 4 11
5 7 10
4
L 3 1
R 2 3
D 5 3
U 4 2

Sample Output 1

36

Stealing Jewel 1, 5, 6 and 7 results in the total value of 36.

Sample Input 2

3
1 2 3
4 5 6
7 8 9
1
L 100 0

Sample Output 2

0

Sample Input 3

4
1 1 10
1 2 11
2 1 12
2 2 13
3
L 8 3
L 9 2
L 10 1

Sample Output 3

13

Sample Input 4

10
66 47 71040136000
65 77 74799603000
80 53 91192869000
24 34 24931901000
91 78 49867703000
68 71 46108236000
46 73 74799603000
56 63 93122668000
32 51 71030136000
51 26 70912345000
21
L 51 1
L 7 0
U 47 4
R 92 0
R 91 1
D 53 2
R 65 3
D 13 0
U 63 3
L 68 3
D 47 1
L 91 5
R 32 4
L 66 2
L 80 4
D 77 4
U 73 1
D 78 5
U 26 5
R 80 2
R 24 5

Sample Output 4

305223377000

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<ll,ll> P;

class minCostFlow {

	struct edge { ll to, cap, cost, rev; };

	ll V;
	vector<vector<edge>> G;
	vector<ll> dist;
	vector<ll> prevv;
	vector<ll> preve;

public:

	minCostFlow(ll n): G(n), dist(n), prevv(n), preve(n), V(n){
	}

	void addEdge(ll from, ll to, ll cap, ll cost) {
		G[from].push_back((edge){to, cap, cost, (ll)G[to].size()});
		G[to].push_back((edge){from, 0, -cost, (ll)G[from].size() - 1});
	}

	ll solve(ll s, ll t, ll f) {
		ll ret = 0;
		while(f > 0) {
			fill(dist.begin(), dist.end(), LLINF);
			dist[s] = 0;
			bool update = true;
			while(update) {
				update = false;
				for(ll v = 0;v < V;v++) {
					if(dist[v] == LLINF)continue;
					for(ll i = 0;i < G[v].size();i++) {
						edge &e = G[v][i];
						if(e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
							dist[e.to] = dist[v] + e.cost;
							prevv[e.to] = v;
							preve[e.to] = i;
							update = true;
						}
					}
				}
			}
			if(dist[t] == LLINF) {
				return LLINF;//流せない
			}

			ll d = f;
			for(ll v = t;v != s;v = prevv[v]) {
				d = min(d, G[prevv[v]][preve[v]].cap);
			}
			f -= d;
			ret += d * dist[t];
			for(ll v = t;v != s;v = prevv[v]) {
				edge &e = G[prevv[v]][preve[v]];
				e.cap -= d;
				G[v][e.rev].cap += d;
			}
		}
		return ret;
	}

};

ll check(vector<pair<P, ll>> &v, vector<pair<char, P>> &t, int k){
	ll cost = 1000000000000000LL;
	int n = v.size();
	int m = t.size();
	
	vector<P> ver(k, MP(0, LLINF));
	vector<P> hor(k, MP(0, LLINF));
	
	REP(i, m){
		char c = t[i].FI;
		P p = t[i].SE;
		if(c == 'L'){
			if(p.SE >= k)continue;
			hor[p.SE].FI = max(hor[p.SE].FI, p.FI + 1);
		}
		else if(c == 'R'){
			if(k - p.SE - 1 < 0)continue;
			hor[k - p.SE - 1].SE = min(hor[k - p.SE - 1].SE, p.FI - 1);
		}
		else if(c == 'D'){
			if(p.SE >= k)continue;
			ver[p.SE].FI = max(ver[p.SE].FI, p.FI + 1);
		}
		else {
			if(k - p.SE - 1 < 0)continue;
			ver[k - p.SE - 1].SE = min(ver[k - p.SE - 1].SE, p.FI - 1);
		}
	}
	
	REP(i, k - 1){
		ver[i + 1].FI = max(ver[i + 1].FI, ver[i].FI);
		hor[i + 1].FI = max(hor[i + 1].FI, hor[i].FI);
		ver[k - i - 2].SE = min(ver[k - i - 2].SE, ver[k - i - 1].SE); 
		hor[k - i - 2].SE = min(hor[k - i - 2].SE, hor[k - i - 1].SE); 
	}
	
	minCostFlow mcf(2 * (n + k) + 10);
	
	ll s = 2 * (k + n);
	ll g = 2 * (k + n) + 1;
	
	REP(i, k){
		ll idA = 2 * n + i;
		ll idB = 2 * n + k + i;
		mcf.addEdge(s, idA, 1, 0);
		mcf.addEdge(idB, g, 1, 0);
	}
	
	REP(i, n){
		P now = v[i].FI;
		ll val = v[i].SE;
		ll idA = i;
		ll idB = i + n;
		mcf.addEdge(idA, idB, 1, cost - val);
		REP(j, k){
			ll idC = 2 * n + j;
			ll idD = 2 * n + k + j;
			if(now.FI >= hor[j].FI && now.FI <= hor[j].SE){
				mcf.addEdge(idC, idA, 1, 0);
			}
			if(now.SE >= ver[j].FI && now.SE <= ver[j].SE){
				mcf.addEdge(idB, idD, 1, 0);
			}
		}
	}
	return k * cost - mcf.solve(s, g, k);
}

int main(){
	
	ll n;cin >> n;
	vector<pair<P, ll>> v(n);
	REP(i, n)cin >> v[i].FI.FI >> v[i].FI.SE >> v[i].SE;
	ll m;cin >> m;
	vector<pair<char, P>> t(m);
	REP(i, m)cin >> t[i].FI >> t[i].SE.FI >> t[i].SE.SE;
	
	ll ans = 0;
	
	for(ll i = 1;i <= n;i++){
		ans = max(ans, check(v, t, i));
	}
	
	cout << ans << endl;
	
	return 0;
}