W=int(input()) n=int(input()) c1=list(map(int,input().split())) m=int(input()) c2=list(map(int,input().split())) S=n+m T=S+1 X=10**10 e=[[0 for j in range(n+m+2)] for i in range(n+m+2)] for i in range(n): e[S][i]=c1[i] for i in range(m): e[n+i][T]=c2[i] for i in range(m): q,*x,=map(int,input().split()) for j in range(n): if j+1 not in x: e[j][n+i]=X w=0 while 1: q=[S] v=[0]*(n+m+2) g=[0]*(n+m+2) f=0 while len(q)>0: s=q[-1] v[s]=1 if s==T: f=1 c=min(e[q[i]][q[i+1]] for i in range(len(q)-1)) w+=c for i in range(len(q)-1): e[q[i]][q[i+1]]-=c e[q[i+1]][q[i]]+=c break while g[s]0: break g[s]+=1 if g[s]=W])