#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 1e3, MAX_COOR = 100;
const ll INF = LLONG_MAX / 2;

void set_max(int &a, int b) { a = max(a, b); }
void set_min(int &a, int b) { a = min(a, b); }

namespace MCMF {
    struct Edge;

    struct Node {
        Edge *from, *head;
        ll dis;
        bool inq;
        Node();
    };
    vector<Node *> nodes;

    Node::Node() : head(nullptr), inq(false) { nodes.push_back(this); }

    struct Edge {
        Node *to;
        Edge *nxt, *rev;
        ll cap, cost;
        Edge(Node *a, Node *b, ll cap, ll cost) : to(b), nxt(a->head), cap(cap), cost(cost) {
            a->head = this;
        }
    };

    void add_edge(Node *a, Node *b, ll cap, ll cost) {
        new Edge(a, b, cap, cost);
        new Edge(b, a, 0, -cost);
        (a->head->rev = b->head)->rev = a->head;
    }

    bool SPFA(Node *S, Node *T) {
        for (Node *i : nodes) i->dis = -INF;
        S->dis = 0;
        queue<Node *> que;
        auto push = [&](Node *x) { que.push(x); x->inq = true; };
        auto pop = [&]() { Node *x = que.front(); que.pop(); x->inq = false; return x; };
        push(S);
        while (!que.empty()) {
            Node *x = pop();
            for (Edge *i = x->head; i; i = i->nxt) {
                if (i->cap && x->dis + i->cost > i->to->dis) {
                    i->to->dis = x->dis + i->cost; i->to->from = i;
                    if (!i->to->inq) {
                        push(i->to);
                    }
                }
            }
        }
        return T->dis != -INF;
    }

    void main(Node *S, Node *T, ll &flow, ll &cost) {
        while (SPFA(S, T)) {
            ll v = INF;
            for (Node *i = T; i != S; i = i->from->rev->to) v = min(v, i->from->cap);
            flow += v; cost += v * T->dis;
            for (Node *i = T; i != S; i = i->from->rev->to) i->from->cap -= v, i->from->rev->cap += v;
        }
    }

    void clear() {
        for (Node *i : nodes) {
            for (Edge *j = i->head, *k; j; j = k) k = j->nxt, delete j;
            delete i;
        }
        nodes.clear();
    }

    void describe_edge(Node *a, Node *b) {
        for (Edge *i = a->head; i; i = i->nxt) {
            if (i->to == b) {
                printf("cap: %lld cost:%lld used: %lld\n", i->cap, i->cost, i->rev->cap);
                return;
            }
        }
        printf("Not found\n");
    }
}

using MCMF::Node;
using MCMF::add_edge;
using MCMF::describe_edge;

struct Jewel {
    int x, y;
    ll v;

    void read() { scanf("%d%d%lld", &x, &y, &v); }
} jewels[MAX_N];

struct Constraint {
    char t;
    int a, b;

    void read() { char s[5]; scanf("%s", s); t = s[0]; scanf("%d%d", &a, &b); }
} constraints[MAX_N];

int N, M;
int x_mx[MAX_N], x_mn[MAX_N], y_mx[MAX_N], y_mn[MAX_N], x_delta[MAX_N], y_delta[MAX_N];
Node *x_n[MAX_N], *y_n[MAX_N], *S, *T;
ll ans = 0;

int main() {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) jewels[i].read();
    scanf("%d", &M);
    for (int i = 1; i <= M; i++) constraints[i].read();
    for (int sel = 1; sel <= N; sel++) {
        for (int i = 0; i <= MAX_COOR; i++) x_mx[i] = INT_MAX, x_mn[i] = 0, y_mx[i] = INT_MAX, y_mn[i] = 0, x_delta[i] = 0, y_delta[i] = 0;
        for (int i = 1; i <= M; i++) {
            switch (constraints[i].t) {
            case 'L': {
                set_max(x_mn[constraints[i].a], sel - constraints[i].b);
            } break;
            case 'R': {
                set_min(x_mx[constraints[i].a - 1], constraints[i].b);
            } break;
            case 'D': {
                set_max(y_mn[constraints[i].a], sel - constraints[i].b);
            } break;
            case 'U': {
                set_min(y_mx[constraints[i].a - 1], constraints[i].b);
            } break;
            }
        }
        bool ok = true;
        for (int i = 0; i <= MAX_COOR; i++) {
            if (x_mn[i] > x_mx[i] || y_mn[i] > y_mx[i])
                ok = false;
        }
        if (!ok) continue;
        ll flow = 0, cost = 0;
        for (int i = 0; i <= MAX_COOR; i++) {
            x_delta[i + 1] += x_mn[i];
            x_delta[i] -= x_mn[i];
            y_delta[i + 1] -= y_mn[i];
            y_delta[i] += y_mn[i];
        }
        x_delta[0] += sel;
        y_delta[0] -= sel;
        for (int i = 0; i <= MAX_COOR + 1; i++) {
            x_n[i] = new Node();
            y_n[i] = new Node();
        }
        S = new Node();
        T = new Node();
        for (int i = 0; i <= MAX_COOR + 1; i++) {
            if (x_delta[i] > 0) {
                add_edge(S, x_n[i], x_delta[i], 0);
                flow -= x_delta[i];
            }
            if (x_delta[i] < 0) {
                add_edge(x_n[i], T, -x_delta[i], 0);
            }
            if (y_delta[i] > 0) {
                add_edge(S, y_n[i], y_delta[i], 0);
                flow -= y_delta[i];
            }
            if (y_delta[i] < 0) {
                add_edge(y_n[i], T, -y_delta[i], 0);
            }
        }
        for (int i = 0; i <= MAX_COOR; i++) {
            add_edge(x_n[i], x_n[i + 1], x_mx[i] - x_mn[i], 0);
            add_edge(y_n[i + 1], y_n[i], y_mx[i] - y_mn[i], 0);
        }
        for (int i = 1; i <= N; i++) {
            add_edge(x_n[jewels[i].x], y_n[jewels[i].y], 1, jewels[i].v);
        }
        MCMF::main(S, T, flow, cost);
        if (flow == 0) {
            ans = max(ans, cost);
        }
        MCMF::clear();
    }
    printf("%lld\n", ans);
}


Measured memory_bytes: 512