import sequtils,strutils type E=tuple[dst,cap,rev:int] proc r:any=stdin.readLine.parseInt proc R:any=stdin.readLine.split.map(parseInt) var w=r() n=r() J=R() m=r() C=R() F=newSeqWith(n+m+2,newSeq[E]()) src=n+m dst=n+m+1 proc a(src,dst,cap:int)= F[src]&=(dst,cap,F[dst].len) F[dst]&=(src,0,F[src].len-1) for i,j in J:a src,i,j for i,c in C:a n+i,dst,c for c in n..0:(F[src][i].cap-=d;F[e.dst][e.rev].cap+=d;return d) while true: U=newSeq[bool](F.len) let f=dfs(src,dst,1e10.int) if f==0:return result+=f if FF()