#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1;char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const int maxn=1020;
const int MAX=100;
const int inf=0x3f3f3f3f;

struct node {
	int v,next,w;ll cost;
};
node e[10200];
int cnt,start[1020];
inline void addedge(int u,int v,int w,ll cost){ e[++cnt]=(node){v,start[u],w,cost}; start[u]=cnt;}
inline void Addedge(int u,int v,int w,ll cost){ addedge(u,v,w,cost); addedge(v,u,0,-cost);}

int tot,S,T;

int n,m,x[maxn],y[maxn];
ll v[maxn];

int L[maxn],R[maxn],D[maxn],U[maxn];

inline void init()
{
	n=read();
	REP(i,1,n)x[i]=read(),y[i]=read(),scanf("%lld",&v[i]);
	REP(i,0,MAX+1)L[i]=R[i]=D[i]=U[i]=n;
	m=read();
	REP(i,1,m)
	{
		char op; scanf("%c",&op); int a=read(),b=read();
		if(op=='L')chkmin(L[a],b);
		else if(op=='R')chkmin(R[a],b);
		else if(op=='D')chkmin(D[a],b);
		else if(op=='U')chkmin(U[a],b);
	}
	REP(i,1,MAX)chkmin(R[i],R[i-1]),chkmin(U[i],U[i-1]);
	DREP(i,MAX,1)chkmin(L[i-1],L[i]),chkmin(D[i-1],D[i]);
}

ll Ans;
ll ans;
int FLOW,EXFLOW;

int exflow[maxn];

inline void Add(int u,int v,int l,int r,int cost)
{
	Addedge(u,v,r-l,cost);
	ans+=l*cost;
	exflow[v]+=l; exflow[u]-=l;
}

inline void build()
{
	REP(i,1,tot)
		if(exflow[i]>0)Addedge(S,i,exflow[i],0),EXFLOW+=exflow[i];
		else if(exflow[i]<0)Addedge(i,T,-exflow[i],0);
}

inline void min_cost(int S,int T)
{
	static ll dist[maxn];
	static int vis[maxn],pre[maxn];
	queue<int>q;
	FLOW=0;
	while(1)
	{
		REP(i,1,tot)dist[i]=1ll<<60,vis[i]=pre[i]=0;
		q.push(S); dist[S]=0;
		while(!q.empty())
		{
			int u=q.front();q.pop();vis[u]=0;
			EREP(i,u)
			{
				int v=e[i].v;
				if(e[i].w && dist[v]>dist[u]+e[i].cost)
				{
					dist[v]=dist[u]+e[i].cost;
					pre[v]=i;
					if(!vis[v])vis[v]=1,q.push(v);
				}
			}
		}
		if(dist[T]==(1ll<<60))break;
		int flow=inf;
		for(int i=pre[T];i;i=pre[e[i^1].v])chkmin(flow,e[i].w);
		for(int i=pre[T];i;i=pre[e[i^1].v])e[i].w-=flow,e[i^1].w+=flow;
		ans+=flow*dist[T]; FLOW+=flow;
	}
}

inline void work(int take)
{
	static int numx[maxn],numy[maxn];
	memset(exflow,0,sizeof(int)*(tot+1));
	memset(start,0,sizeof(int)*(tot+1)); tot=0; cnt=1;
	S=++tot; T=++tot;
	REP(i,0,MAX)numx[i]=++tot,numy[i]=++tot;
	Addedge(S,numx[0],take,0);
	Addedge(numy[0],T,take,0);
	REP(i,1,MAX)
	{
		int low=max(0,take-L[i-1]),high=R[i];
		if(low>high)return;
		Add(numx[i-1],numx[i],low,high,0);
	}
	REP(i,1,MAX)
	{
		int low=max(0,take-D[i-1]),high=U[i];
		if(low>high)return;
		Add(numy[i],numy[i-1],low,high,0);
	}
	REP(i,1,n)Addedge(numx[x[i]],numy[y[i]],1,-v[i]);
	ans=EXFLOW=0; 
	build();
	min_cost(S,T);
	if(EXFLOW+take==FLOW)chkmax(Ans,-ans);
}

inline void doing()
{
	REP(i,0, min(min(L[MAX],D[MAX]),min(R[0],U[0])) )work(i);
	printf("%lld\n",Ans);
}

int main()
{
	init();
	doing();
	return 0;
}


Measured memory_bytes: 384