import itertools
import gurobipy as gp
from gurobipy import GRB
import sys

def solve_one():
    e, n = [ int(_) for _ in input().split() ]
    S = [ int(_) for _ in input().split() ]
    V = [ int(_) for _ in input().split() ]
    
    z = int(input())
    D = [ [ int(_)-1 for _ in input().split() ] for __ in range(z) ]

    k = int(input())
    C = [ [ int(_)-1 for _ in input().split() ] for __ in range(k) ] # -1 lebo interne cislujeme od 0

    q = int(input())
    T = [ int(_) for _ in input().split() ]
    ignore = input()
    U = [ [ int(_)-1 for _ in input().split() ] for __ in range(q) ]

    env = gp.Env(empty=True)
    env.setParam('OutputFlag', 0)
    env.start()
    model = gp.Model('investicie', env=env)
    model.setParam('MIPGap', 0.0)
    project_vars = model.addVars(n, vtype=GRB.BINARY, name='project')
    synergy_vars = model.addVars(q, vtype=GRB.BINARY, name='synergy')

    model.addConstr( gp.quicksum( project_vars[i] * S[i] for i in range(n) ) <= e )
    for conflict in C:
        model.addConstr( gp.quicksum( project_vars[i] for i in conflict ) <= 1 )
    for dependency in D:
        model.addConstr( project_vars[dependency[0]] <= project_vars[dependency[1]] )
    for i, inputs in enumerate(U):
        for x in inputs: model.addConstr( synergy_vars[i] <= project_vars[x] )
        model.addConstr( synergy_vars[i] >= gp.quicksum( project_vars[x] for x in inputs ) - len(inputs) + 1 )

    model.setObjective( 
        e + 
        gp.quicksum( project_vars[i] * (V[i] - S[i]) for i in range(n) ) + 
        gp.quicksum( synergy_vars[j] * T[j] for j in range(q) ),
        GRB.MAXIMIZE
    )
    
    model.optimize()
    assert model.status == GRB.OPTIMAL
    print( int(round(model.objVal)) )

if __name__ == '__main__':
    tc = int( input() )
    for test in range(tc): solve_one()

