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