n=gets.to_i l=n*4 g=(1..l).map{[0]*l} a=0 (0...n).map{|u| b,c=gets.split a+=g[0][2+u]=b.to_i a+=g[2+n+u][1]=c.to_i g[2+u][2+n+u]=9999 } gets.to_i.times{ d,e=gets.split g[2+d.to_i][2+n+e.to_i]=9999 } ( q=[0] p=[-1]*l ( u=q.shift l.times{|v| ( p[v]=u q<0 } )while q.any? break if p[1]<0 m=9999 v=1 (m=[m,g[u=p[v]][v]].min;v=u)while v>0 a-=m v=1 (g[u=p[v]][v]-=m;g[v][u]+=m;v=u)while v>0 )while !!n p a