#include <cassert>
#include <iostream>
#include <vector>
#include <cmath>    // Added for std::round
#include <cstdio>   // Added for snprintf
#include "lp_lib.h" // The lp_solve header file

using namespace std;

void solve_one() {
    int e, n;
    cin >> e >> n;

    vector<int> S(n), V(n);
    for (int &s : S) cin >> s;
    for (int &v : V) cin >> v;

    int z;
    cin >> z;
    vector< vector<int> > D(z, vector<int>(2));
    for (int i = 0; i < z; ++i) cin >> D[i][0] >> D[i][1];

    int k;
    cin >> k;
    vector< vector<int> > C(k, vector<int>(2));
    for (int i = 0; i < k; ++i) cin >> C[i][0] >> C[i][1];

    int q;
    cin >> q;
    vector<int> T(q);
    for (int &t : T) cin >> t;
    vector<int> L(q);
    for (int &l : L) cin >> l;
    vector< vector<int> > U(q);
    for (int i = 0; i < q; ++i) {
        U[i].resize(L[i]);
        for (int &u : U[i]) cin >> u;
    }
    
    // Create a new LP problem with 0 rows (constraints) and n+q columns (variables)
    lprec *lp = make_lp(0, n + q); 
    assert(lp != NULL);

    for (int i = 0; i < n; ++i) { 
        char name[20]; 
        snprintf(name, 19, "x%d", i + 1); 
        set_col_name(lp, i + 1, name); 
    }
    for (int i = n; i < n + q; ++i) { 
        char name[20]; 
        snprintf(name, 19, "s%d", i - n + 1); 
        set_col_name(lp, i + 1, name); 
    }

    // Define the objective function (Using std::vector instead of VLA)
    std::vector<REAL> objective_coefs(n + q + 1, 0.0);
    for (int i = 0; i < n; ++i) objective_coefs[i + 1] = V[i] - S[i];
    for (int i = 0; i < q; ++i) objective_coefs[n + i + 1] = T[i];
    set_obj_fn(lp, objective_coefs.data());
    set_maxim(lp);

    // Set all variables to binary
    for (int i = 0; i < n + q; ++i) set_binary(lp, i + 1, TRUE);

    // Add the budget constraint
    {
        std::vector<REAL> constraint(n + q + 1, 0.0);
        for (int j = 0; j < n; ++j) constraint[j + 1] = S[j];
        add_constraint(lp, constraint.data(), LE, e);
    }
    
    // Add conflict constraints
    for (auto conflict : C) {
        std::vector<REAL> constraint(n + q + 1, 0.0);
        constraint[conflict[0]] = 1;
        constraint[conflict[1]] = 1;
        add_constraint(lp, constraint.data(), LE, 1);
    }
    
    // Add dependency constraints
    for (auto dependency : D) {
        std::vector<REAL> constraint(n + q + 1, 0.0);
        constraint[dependency[0]] = 1;
        constraint[dependency[1]] = -1;
        add_constraint(lp, constraint.data(), LE, 0);
    }
    
    // Add synergy constraints
    for (int i = 0; i < q; ++i) {
        vector<int> group = U[i];
        
        // Synergy variable <= each required project
        for (int x : group) {
            std::vector<REAL> constraint(n + q + 1, 0.0);
            constraint[n + 1 + i] = 1;
            constraint[x] = -1;
            add_constraint(lp, constraint.data(), LE, 0);
        }
        
        // Synergy variable >= sum(projects) - length + 1
        {
            std::vector<REAL> constraint(n + q + 1, 0.0);
            constraint[n + 1 + i] = 1;
            for (int x : group) constraint[x] = -1;
            add_constraint(lp, constraint.data(), GE, 1 - int(group.size()));
        }
    }
    
    set_verbose(lp, IMPORTANT);
    set_presolve(lp, PRESOLVE_ROWS | PRESOLVE_COLS | PRESOLVE_LINDEP, get_presolveloops(lp));
    
    // Solve the model
    int result_code = solve(lp);
    assert(result_code == OPTIMAL);
    
    // Extract variables and manually calculate exact objective 
    // to bypass lp_solve's floating-point accumulation errors
    std::vector<REAL> var_values(n + q, 0.0);
    get_variables(lp, var_values.data());

    // Use long long to safely avoid any 32-bit integer limits
    long long exact_objective = e;
    
    for (int i = 0; i < n; ++i) {
        if (std::round(var_values[i]) == 1.0) {
            exact_objective += (V[i] - S[i]);
        }
    }
    for (int i = 0; i < q; ++i) {
        if (std::round(var_values[n + i]) == 1.0) {
            exact_objective += T[i];
        }
    }

    cout << exact_objective << endl;

    if (lp != NULL) delete_lp(lp);
}

int main() {
    int tc;
    cin >> tc;
    while (tc--) solve_one();
}
