#include<algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000007;
ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
template<class T>inline void gmax(T &A, T B) {
(A<B)&&(A=B);//if(B>A)A=B;
}
template<class T>inline void gmin(T &A, T B) {
(A>B)&&(A=B);//if(B<A)A=B;
}
template <class T>
inline bool scan_d(T &ret) {
char c;
int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } inline void outt(int x) { if(x>9) outt(x/10);
putchar(x%10+'0');
}
const int inf=1<<29,N=50010,M=300010;
int head[N],ver[M],edge[M],Next[M],d[N];
int n,np,nc,m,s,t,tot,maxflow;
queue<int>q;
void add(int x,int y,int z) {
ver[++tot]=y;
edge[tot]=z;
Next[tot]=head[x];
head[x]=tot;
ver[++tot]=x;
edge[tot]=0;
Next[tot]=head[y];
head[y]=tot;
}
bool bfs() { //在残量网络上构造分层图
memset(d,0,sizeof(d));
while(q.size())q.pop();
q.push(s);
d[s]=1;
while(q.size()) {
int x=q.front();
q.pop();
for(int i=head[x]; i; i=Next[i])
if(edge[i]&&!d[ver[i]]) {
q.push(ver[i]);
d[ver[i]]=d[x]+1;
if(ver[i]==t)return 1;
}
}
return 0;
}
int dinic(int x,int flow) { //在当前分层图上增广
if(x==t)return flow;
int rest=flow,k;
for(int i=head[x]; i; i=Next[i])
if(edge[i]&&d[ver[i]]==d[x]+1) {
k=dinic(ver[i],min(rest,edge[i]));
if(!k)d[ver[i]]=0;
edge[i]-=k;
edge[i^1]+=k;
rest-=k;
}
return flow-rest;
}
void init() {
maxflow=0;
tot=1;
memset(head,0,sizeof(head));
}
int main() {
while(scanf("%d%d%d%d", &n, &np, &nc, &m)!=EOF) {
init();
int u,v,w;
char tmp;
s=0,t=n+1;
rep(i,1,m){
scanf(" %c%d%c%d%c%d", &tmp,&u,&tmp,&v,&tmp,&w);
++u,++v;
add(u,v,w);
}
rep(i,1,np){
scanf(" %c%d%c%d", &tmp,&u,&tmp,&w);
++u;
add(s,u,w);
}
rep(i,1,nc){
scanf(" %c%d %c%d", &tmp,&v,&tmp,&w);
++v;
add(v,t,w);
}
int flow=0;
while(bfs()){
while(flow=dinic(s,inf))maxflow+=flow;
}
printf("%d\n", maxflow);
}
return 0;
}