#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1111;
const int INF = 1e9;
struct Edge {
int to, rev, cap, cost, f = 0;
Edge(int to, int rev, int cap, int cost) : to(to), rev(rev), cap(cap), cost(cost) {}
};
vector<Edge> g[N];
int nodes, src, dest;
int prio[N], curFlow[N], prevEdge[N], prevNode[N], pot[N] = {};
bool used[N];
void addEdge(int from, int to, int cap, int cost) {
g[from].pb(Edge(to, (int)g[to].size(), cap, cost));
g[to].pb(Edge(from, (int)g[from].size() - 1, 0, -cost));
}
PII minCostFlow(int maxFlow) {
int flow = 0;
int flowCost = 0;
REP(i, nodes) pot[i] = 0;
while (flow < maxFlow) {
REP(i, nodes) prio[i] = INF;
prio[src] = 0;
curFlow[src] = INF;
REP(i, nodes) used[i] = false;
while (true) {
int v = -1;
REP(i, nodes) if (!used[i] && (v == -1 || prio[v] > prio[i])) {
v = i;
}
if (v == -1 || prio[v] == INF) break;
used[v] = true;
REP(i, g[v].size()) {
Edge &e = g[v][i];
if (e.f >= e.cap) continue;
int newPrio = prio[v] + e.cost + pot[v] - pot[e.to];
if (prio[e.to] > newPrio) {
prio[e.to] = newPrio;
prevNode[e.to] = v;
prevEdge[e.to] = i;
curFlow[e.to] = min(curFlow[v], e.cap - e.f);
}
}
}
if (prio[dest] == INF) break;
REP(i, nodes) if (prio[i] < INF) {
pot[i] += prio[i];
}
int df = min(curFlow[dest], maxFlow - flow);
flow += df;
flowCost += df * pot[dest];
for (int v = dest; v != src; v = prevNode[v]) {
Edge &e = g[prevNode[v]][prevEdge[v]];
e.f += df;
g[v][e.rev].f -= df;
}
}
return mp(flow, flowCost);
}
int d[101];
int n, k, c, m, qqq;
int from[500], to[500], w[500];
int ko[100][15];
int cur[15];
int solve(int x, int y) {
src = n + k;
dest = src + 1;
nodes = dest + 1;
REP(i, nodes) g[i].clear();
REP(i, k) cur[i] = 0;
for (int i = x; i <= y; ++i) REP(j, k) {
cur[j] += ko[i][j];
}
REP(i, m) addEdge(from[i], n + to[i], 1, w[i] * cur[to[i]]);
int nz = 0;
REP(i, k) if (cur[i] > 0) {
addEdge(n + i, dest, 1, 0);
++nz;
}
REP(i, n) {
addEdge(src, i, 1, 0);
}
PII res = minCostFlow(nz);
if (res.first != nz) return INF;
return res.second + c;
}
int main() {
scanf("%d%d%d%d", &n, &k, &c, &m);
REP(i, m) {
scanf("%d%d%d", from + i, to + i, w + i);
--from[i], --to[i];
}
scanf("%d", &qqq);
REP(i, qqq) REP(j, k) scanf("%d", ko[i] + j);
d[0] = 0;
for (int i = 1; i <= qqq; ++i) d[i] = INF;
REP(i, qqq) {
for (int j = i; j < qqq; ++j) {
d[j + 1] = min(d[j + 1], d[i] + solve(i, j));
}
}
printf("%d\n", d[qqq]);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBSRVAoaSwgbikgZm9yIChpbnQgaSA9IDA7IGkgPCAoaW50KShuKTsgKytpKQp0eXBlZGVmIGxvbmcgbG9uZyBMTDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBQSUk7Cgpjb25zdCBpbnQgTiA9IDExMTE7CmNvbnN0IGludCBJTkYgPSAxZTk7CgpzdHJ1Y3QgRWRnZSB7CiAgICBpbnQgdG8sIHJldiwgY2FwLCBjb3N0LCBmID0gMDsKICAgIEVkZ2UoaW50IHRvLCBpbnQgcmV2LCBpbnQgY2FwLCBpbnQgY29zdCkgOiB0byh0byksIHJldihyZXYpLCBjYXAoY2FwKSwgY29zdChjb3N0KSB7fQp9OwoKdmVjdG9yPEVkZ2U+IGdbTl07CmludCBub2Rlcywgc3JjLCBkZXN0OwppbnQgcHJpb1tOXSwgY3VyRmxvd1tOXSwgcHJldkVkZ2VbTl0sIHByZXZOb2RlW05dLCBwb3RbTl0gPSB7fTsKYm9vbCB1c2VkW05dOwoKdm9pZCBhZGRFZGdlKGludCBmcm9tLCBpbnQgdG8sIGludCBjYXAsIGludCBjb3N0KSB7CiAgICBnW2Zyb21dLnBiKEVkZ2UodG8sIChpbnQpZ1t0b10uc2l6ZSgpLCBjYXAsIGNvc3QpKTsKICAgIGdbdG9dLnBiKEVkZ2UoZnJvbSwgKGludClnW2Zyb21dLnNpemUoKSAtIDEsIDAsIC1jb3N0KSk7Cn0KClBJSSBtaW5Db3N0RmxvdyhpbnQgbWF4RmxvdykgewogICAgaW50IGZsb3cgPSAwOwogICAgaW50IGZsb3dDb3N0ID0gMDsKICAgIFJFUChpLCBub2RlcykgcG90W2ldID0gMDsKICAgIHdoaWxlIChmbG93IDwgbWF4RmxvdykgewogICAgICAgIFJFUChpLCBub2RlcykgcHJpb1tpXSA9IElORjsKICAgICAgICBwcmlvW3NyY10gPSAwOwogICAgICAgIGN1ckZsb3dbc3JjXSA9IElORjsKICAgICAgICBSRVAoaSwgbm9kZXMpIHVzZWRbaV0gPSBmYWxzZTsKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpbnQgdiA9IC0xOwogICAgICAgICAgICBSRVAoaSwgbm9kZXMpIGlmICghdXNlZFtpXSAmJiAodiA9PSAtMSB8fCBwcmlvW3ZdID4gcHJpb1tpXSkpIHsKICAgICAgICAgICAgICAgIHYgPSBpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh2ID09IC0xIHx8IHByaW9bdl0gPT0gSU5GKSBicmVhazsKICAgICAgICAgICAgdXNlZFt2XSA9IHRydWU7CiAgICAgICAgICAgIFJFUChpLCBnW3ZdLnNpemUoKSkgewogICAgICAgICAgICAgICAgRWRnZSAmZSA9IGdbdl1baV07CiAgICAgICAgICAgICAgICBpZiAoZS5mID49IGUuY2FwKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGludCBuZXdQcmlvID0gcHJpb1t2XSArIGUuY29zdCArIHBvdFt2XSAtIHBvdFtlLnRvXTsKICAgICAgICAgICAgICAgIGlmIChwcmlvW2UudG9dID4gbmV3UHJpbykgewogICAgICAgICAgICAgICAgICAgIHByaW9bZS50b10gPSBuZXdQcmlvOwogICAgICAgICAgICAgICAgICAgIHByZXZOb2RlW2UudG9dID0gdjsKICAgICAgICAgICAgICAgICAgICBwcmV2RWRnZVtlLnRvXSA9IGk7CiAgICAgICAgICAgICAgICAgICAgY3VyRmxvd1tlLnRvXSA9IG1pbihjdXJGbG93W3ZdLCBlLmNhcCAtIGUuZik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHByaW9bZGVzdF0gPT0gSU5GKSBicmVhazsKICAgICAgICBSRVAoaSwgbm9kZXMpIGlmIChwcmlvW2ldIDwgSU5GKSB7CiAgICAgICAgICAgIHBvdFtpXSArPSBwcmlvW2ldOwogICAgICAgIH0KICAgICAgICBpbnQgZGYgPSBtaW4oY3VyRmxvd1tkZXN0XSwgbWF4RmxvdyAtIGZsb3cpOwogICAgICAgIGZsb3cgKz0gZGY7CiAgICAgICAgZmxvd0Nvc3QgKz0gZGYgKiBwb3RbZGVzdF07CiAgICAgICAgZm9yIChpbnQgdiA9IGRlc3Q7IHYgIT0gc3JjOyB2ID0gcHJldk5vZGVbdl0pIHsKICAgICAgICAgICAgRWRnZSAmZSA9IGdbcHJldk5vZGVbdl1dW3ByZXZFZGdlW3ZdXTsKICAgICAgICAgICAgZS5mICs9IGRmOwogICAgICAgICAgICBnW3ZdW2UucmV2XS5mIC09IGRmOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBtcChmbG93LCBmbG93Q29zdCk7Cn0KCmludCBkWzEwMV07CmludCBuLCBrLCBjLCBtLCBxcXE7CmludCBmcm9tWzUwMF0sIHRvWzUwMF0sIHdbNTAwXTsKaW50IGtvWzEwMF1bMTVdOwppbnQgY3VyWzE1XTsKCmludCBzb2x2ZShpbnQgeCwgaW50IHkpIHsKICAgIHNyYyA9IG4gKyBrOwogICAgZGVzdCA9IHNyYyArIDE7CiAgICBub2RlcyA9IGRlc3QgKyAxOwogICAgUkVQKGksIG5vZGVzKSBnW2ldLmNsZWFyKCk7CiAgICBSRVAoaSwgaykgY3VyW2ldID0gMDsKICAgIGZvciAoaW50IGkgPSB4OyBpIDw9IHk7ICsraSkgUkVQKGosIGspIHsKICAgICAgICBjdXJbal0gKz0ga29baV1bal07CiAgICB9CiAgICBSRVAoaSwgbSkgYWRkRWRnZShmcm9tW2ldLCBuICsgdG9baV0sIDEsIHdbaV0gKiBjdXJbdG9baV1dKTsKICAgIGludCBueiA9IDA7CiAgICBSRVAoaSwgaykgaWYgKGN1cltpXSA+IDApIHsKICAgICAgICBhZGRFZGdlKG4gKyBpLCBkZXN0LCAxLCAwKTsKICAgICAgICArK256OwogICAgfQogICAgUkVQKGksIG4pIHsKICAgICAgICBhZGRFZGdlKHNyYywgaSwgMSwgMCk7CiAgICB9CiAgICBQSUkgcmVzID0gbWluQ29zdEZsb3cobnopOwogICAgaWYgKHJlcy5maXJzdCAhPSBueikgcmV0dXJuIElORjsKICAgIHJldHVybiByZXMuc2Vjb25kICsgYzsKfQoKaW50IG1haW4oKSB7CiAgICBzY2FuZigiJWQlZCVkJWQiLCAmbiwgJmssICZjLCAmbSk7CiAgICBSRVAoaSwgbSkgewogICAgICAgIHNjYW5mKCIlZCVkJWQiLCBmcm9tICsgaSwgdG8gKyBpLCB3ICsgaSk7CiAgICAgICAgLS1mcm9tW2ldLCAtLXRvW2ldOwogICAgfQogICAgc2NhbmYoIiVkIiwgJnFxcSk7CiAgICBSRVAoaSwgcXFxKSBSRVAoaiwgaykgc2NhbmYoIiVkIiwga29baV0gKyBqKTsKICAgIGRbMF0gPSAwOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gcXFxOyArK2kpIGRbaV0gPSBJTkY7CiAgICBSRVAoaSwgcXFxKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IGk7IGogPCBxcXE7ICsraikgewogICAgICAgICAgICBkW2ogKyAxXSA9IG1pbihkW2ogKyAxXSwgZFtpXSArIHNvbHZlKGksIGopKTsKICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVkXG4iLCBkW3FxcV0pOwogICAgcmV0dXJuIDA7Cn0K