import pulp

def validate(e, n, S, V, D, C, T, U, answer, project, synergy):
    real_answer = e + sum( project[i] * (V[i] - S[i]) for i in range(n) ) + sum( synergy[j] * T[j] for j in range(len(T)) )
    assert real_answer == answer
    assert sum( project[i] * S[i] for i in range(n) ) <= e
    for conflict in C:
        assert sum( project[i] for i in conflict ) <= 1
    for dependency in D:
        assert project[dependency[0]] <= project[dependency[1]]
    for svar, inputs in zip(synergy, U):
        if svar:
            assert all( project[x] for x in inputs )
        else:
            assert not all( project[x] for x in inputs )

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) ]

    problem = pulp.LpProblem('Investicie', pulp.LpMaximize)
    project = [ pulp.LpVariable(f'project{i}', cat='Binary') for i in range(n) ]
    synergy = [ pulp.LpVariable(f'synergy{i}', cat='Binary') for i in range(q) ]

    problem += e + sum( project[i] * (V[i] - S[i]) for i in range(n) ) + sum( synergy[j] * T[j] for j in range(q) )
    problem += sum( project[i] * S[i] for i in range(n) ) <= e
    for conflict in C:
        problem += sum( project[i] for i in conflict ) <= 1
    for dependency in D:
        problem += project[dependency[0]] <= project[dependency[1]]
    for sval, inputs in zip(synergy, U):
        for x in inputs: problem += sval <= project[x]
        problem += sval >= sum( project[x] for x in inputs ) - len(inputs) + 1

    status = problem.solve(pulp.PULP_CBC_CMD(msg=0))
    assert pulp.LpStatus[status] == 'Optimal'
    answer = int(round( pulp.value(problem.objective) ))
    print(answer)
    project_vals = [ int(round(pulp.value(x))) for x in project ]
    synergy_vals = [ int(round(pulp.value(x))) for x in synergy ]
    validate(e, n, S, V, D, C, T, U, answer, project_vals, synergy_vals)

tc = int( input() )
for test in range(tc): solve_one()
