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)

//Zory-2019
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define double long double
	const int INF=0x3f3f3f3f;
	const ll LLINF=0x3f3f3f3f3f3f3f3fll;
	ll qread()
	{
		ll ans=0;char c=getchar();int f=1;
		while(c<'0' or c>'9') {if(c=='-') f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) {num=-num;putchar('-');}
		if(num>9) write(num/10);
		putchar('0'+num%10);
	}
	void writeln(int num){write(num);puts("");}
	#define FR first
	#define SE second
	#define MP make_pair
	#define pr pair<int,int>
	#define PB push_back
	inline void chmax(int &x,int y) {x=x>y?x:y;}
	inline void chmin(int &x,int y) {x=x<y?x:y;}

	const int MAX_N=1100,MAX_M=1e5;
	int hou[MAX_N];
	struct Edge{int y,g,c;ll w;}e[MAX_M*2];
	int ln=0;int oth(int x){return x&1?x+1:x-1;}
	void ins(int x,int y,int c,ll w)
	{
		if(c==0) return;
		e[++ln]=(Edge){y,hou[x],c,w};hou[x]=ln;
		e[++ln]=(Edge){x,hou[y],0,-w};hou[y]=ln;
	}
	int st,ed;queue<int> qq;bool v[MAX_N];
	ll dis[MAX_N];int fm[MAX_N],mic[MAX_N];
	ll ans;int flow;
	bool solve()
	{
		memset(dis,-0x3f,sizeof dis);
		memset(v,0,sizeof v);v[st]=1;
		qq.push(st);mic[st]=INF;dis[st]=0;
		while(qq.size())
		{
			int x=qq.front();qq.pop();
			for(int k=hou[x];k>0;k=e[k].g)
			{
				int y=e[k].y;
				if(dis[y]<dis[x]+e[k].w and e[k].c)
				{
					dis[y]=dis[x]+e[k].w;
					mic[y]=min(mic[x],e[k].c);fm[y]=k;
					if(!v[y]) v[y]=1,qq.push(y);
				}
			}
			v[x]=0;
		}
		if(mic[ed]==0 or dis[ed]<0) return 0;
		ans+=dis[ed]*mic[ed];flow+=mic[ed];
		for(int x=ed;x!=st;x=e[oth(fm[x])].y) e[fm[x]].c-=mic[ed],e[oth(fm[x])].c+=mic[ed];
		return 1;
	}

	int n,m;ll val[MAX_N];
	pr pt[MAX_N];struct Qes{int a,b;char op[10];}q[MAX_N];
	int fl[2][MAX_N],fr[2][MAX_N];
	ll getans(int T)
	{
		ln=0;memset(hou,0,sizeof hou);st=0,ed=n*4+1;
		for(int i=1;i<=n;i++) ins(i,n+i,1,val[i]);
		memset(fl,0,sizeof fl);memset(fr,0x3f,sizeof fr);fl[0][1]=1;fr[0][T]=100;fl[1][1]=1;fr[1][T]=100;
		for(int i=1;i<=m;i++) if(q[i].b<T)
		{
			if(q[i].op[0]=='L') chmax(fl[0][q[i].b+1],q[i].a+1);
			if(q[i].op[0]=='R') chmin(fr[0][T-q[i].b],q[i].a-1);
			if(q[i].op[0]=='D') chmax(fl[1][q[i].b+1],q[i].a+1);
			if(q[i].op[0]=='U') chmin(fr[1][T-q[i].b],q[i].a-1);
		}
		for(int i=1;i<T;i++) chmax(fl[0][i+1],fl[0][i]);for(int i=T;i>=2;i--) chmin(fr[0][i-1],fr[0][i]);
		for(int i=1;i<=T;i++)
		{
			ins(st,n*2+i,1,0);
			for(int j=1;j<=n;j++) if(fl[0][i]<=pt[j].FR and pt[j].FR<=fr[0][i]) ins(n*2+i,j,1,0)/*,printf("%d ",j)*/;
			// puts("");
		}
		for(int i=1;i<T;i++) chmax(fl[1][i+1],fl[1][i]);for(int i=T;i>=2;i--) chmin(fr[1][i-1],fr[1][i]);
		for(int i=1;i<=T;i++)
		{
			ins(n*3+i,ed,1,0);
			for(int j=1;j<=n;j++) if(fl[1][i]<=pt[j].SE and pt[j].SE<=fr[1][i]) ins(n+j,n*3+i,1,0)/*,printf("%d ",j)*/;
			// puts("");
		}
		ans=0;flow=0;while(solve()) ;
		// printf("T=%d flow=%d ans=%lld\n",T,flow,ans);
		return ans*(flow==T);
	}
	void main()
	{
		n=qread();for(int i=1;i<=n;i++) scanf("%d%d%lld",&pt[i].FR,&pt[i].SE,&val[i]);
		m=qread();for(int i=1;i<=m;i++) scanf("%s%d%d",q[i].op,&q[i].a,&q[i].b);
		ll ans=0;for(int T=1;T<=n;T++) ans=max(ans,getans(T));
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}