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: Wrong Answer)

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
using namespace std;
 
template <typename T>
using ordered_set =
	tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
#define shandom_ruffle random_shuffle
 
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=327;
using bn=bitset<87>;
using ld=long double;

int n;
pii gdz[nax];
ll war[nax];

int m;
bn podz[nax];
int ile[nax];

ll wyn;

bn ter;
 
ll dajwar()
{
	ll ret=0;
	for (int i=1; i<=n; i++)
		if (ter[i])
			ret+=war[i];
	return ret;
}
 
int popr()
{
	for (int i=1; i<=m; i++)
		if ((podz[i]&ter).count()>ile[i])
			return 0;
	return 1;
}

vi kol;

struct Simplex {        // Maximize c*x subject to Ax <= b.
  using T = ld;     // Initialize the structure, set A, b, c and then run
  vector<vector<T>> A;  // solve(). Max objective is stored in res. To recover
  vector<T> b, c;       // the best result, use getVars().
  int V, E;
  vector<int> eqIds, varIds, cols;
  T res;
  static constexpr T kEps = 1e-8;

  Simplex(int vars, int eqs) : A(eqs, vector<T>(vars)), b(eqs), c(vars),
      V(vars), E(eqs), eqIds(eqs), varIds(vars), res(0) {
    iota(varIds.begin(), varIds.end(), 0);
    iota(eqIds.begin(), eqIds.end(), vars);
  }

  void pivot(int eq, int var) {
    T coef = 1 / A[eq][var];
    cols.clear();
    for (int i = 0; i < V; i++) {
      if (abs(A[eq][i]) > kEps) { cols.push_back(i); A[eq][i] *= coef; }
    }
    A[eq][var] *= coef; b[eq] *= coef;
    for (int row = 0; row < E; row++) {
      if (row == eq || abs(A[row][var]) < kEps) { continue; }
      T k = -A[row][var];
      A[row][var] = 0;
      for (int i : cols) { A[row][i] += k * A[eq][i]; }
      b[row] += k * b[eq];
    }
    T q = c[var]; c[var] = 0;
    for (int i : cols) { c[i] -= q * A[eq][i]; }
    res += q * b[eq];
    swap(varIds[var], eqIds[eq]);
  }

  bool solve() {
    while (true) {
      int eq = -1, var = -1;
      for (int i = 0; i < E; i++) { if (b[i] < -kEps) { eq = i; break; } }
      if (eq == -1) { break; }
      for (int i = 0; i < V; i++) { if (A[eq][i] < -kEps) { var = i; break; } }
      if (var == -1) { res = -1e9; return false; /* No solution */ }
      pivot(eq, var);
    }
    while (true) {
      int var = -1, eq = -1;
      for (int i = 0; i < V; i++) { if (c[i] > kEps) { var = i; break; } }
      if (var == -1) { break; }
      for (int i = 0; i < E; i++) {
        if (A[i][var] < kEps) { continue; }
        if (eq >= 0 && b[i] / A[i][var] >= b[eq] / A[eq][var]) { continue; }
        eq = i;
      }
      if (eq == -1) { res = 1e9; return false; /* Unbounded */ }
      pivot(eq, var);
    }
    return true;
  }

  vector<T> getVars() { // Optimal assignment of variables.
    vector<T> result(V);
    for (int i = 0; i < E; i++) if (eqIds[i] < V) result[eqIds[i]] = b[i];
    return result;
  }
};

void zmien(ld &v)
{
	v*=(800+(rand()%401))/1000.0;
}

int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%d%d%lld", &gdz[i].first, &gdz[i].second, &war[i]);
	scanf("%d", &m);
	for (int i=1; i<=m; i++)
	{
		char c;
		int a, b;
		scanf(" %c%d%d", &c, &a, &b);
		for (int j=1; j<=n; j++)
		{
			if (c=='L' && gdz[j].first<=a)
				podz[i][j]=1;
			if (c=='R' && gdz[j].first>=a)
				podz[i][j]=1;
			if (c=='D' && gdz[j].second<=a)
				podz[i][j]=1;
			if (c=='U' && gdz[j].second>=a)
				podz[i][j]=1;
		}
		ile[i]=b;
	}
	for (int h=0; h<200; h++)
	{
		Simplex janusz(n, m+2*n);
		for (int i=0; i<n; i++)
		{
			janusz.c[i]=war[i+1];
			if (h)
				zmien(janusz.c[i]);
		}
		for (int i=0; i<m; i++)
		{
			janusz.b[i]=ile[i+1];
			for (int j=0; j<n; j++)
				janusz.A[i][j]=podz[i+1][j+1];
		}
		for (int i=0; i<n; i++)
		{
			janusz.b[m+i]=1;
			janusz.A[m+i][i]=1;
		}
		for (int i=0; i<n; i++)
		{
			janusz.b[m+n+i]=0;
			janusz.A[m+n+i][i]=-1;
		}
		assert(janusz.solve());
		auto wez=janusz.getVars();
		
		vector <pair<ld,int> > wek;
		for (int i=1; i<=n; i++)
			wek.push_back({wez[i-1], i});
		
		sort(wek.begin(), wek.end());
		reverse(wek.begin(), wek.end());
		
		ter.reset();
		
		for (auto i : wek)
		{
			ter[i.second]=1;
			if (!popr())
				ter[i.second]=0;
		}
		wyn=max(wyn, dajwar());
	}
	
	printf("%lld\n", wyn);
	return 0;
}