#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }
#define DEBUG_ARR(arr, l, r) { \
cerr << "* "#arr"[" << l << ", " << r << "]:"; \
for(auto it = l, itr = r; it <= itr; it++) \
cerr << arr[it] << ", "; \
cerr << endl; \
}
#define DEBUG_FMT(...) { cerr << "* "; fprintf(stderr, __VA_ARGS__); }
#define DEBUG_HERE { DEBUG_FMT("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__); }
using namespace std;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m;
int a[N], b[N], c[N];
vector<int> e[N];
int one[N], idx;
int ver[N], nxt[N], w[N], cap[N];
void addEdge(int a, int b, int c, int d)
{
ver[idx] = b; cap[idx] = c; w[idx] = d; nxt[idx] = one[a]; one[a] = idx++;
ver[idx] = a; cap[idx] = 0; w[idx] = -d; nxt[idx] = one[b]; one[b] = idx++;
}
int S, T;
int flow, cost;
int pre[N];
bool vis[N];
double min_cost;
int min_pre[N];
int cnt_t = 0;
void dfs(int x, double cur_cost)
{
cnt_t++;
assert(cnt_t <= 100000)
// if(cnt_t > 100000) return;
// DEBUG_VAR(x);
if(cur_cost >= min_cost) return;
if(x == T) {
min_cost = cur_cost;
for(int i = 1; i <= T; i++) {
min_pre[i] = pre[i];
}
return;
}
vis[x] = true;
for(int i = one[x]; ~i; i = nxt[i]) {
// DEBUG_VAR(i);
int y = ver[i];
double z = w[i];
if(vis[y] || !cap[i]) continue;
if(w[i] == 1) {
if(cap[i] % 2 == 0) {
z = 1.9999999;
} else {
z = 0;
}
} else if(w[i] == -1) {
if(cap[i] % 2 == 0) {
z = 0;
} else {
z = -2.000001;
}
}
pre[y] = i;
dfs(y, cur_cost + z);
pre[y] = -1;
}
vis[x] = false;
}
bool EK()
{
fill_n(pre + 1, T, -1);
fill_n(min_pre + 1, T, -1);
min_cost = INF;
cnt_t = 0;
dfs(S, 0);
DEBUG_VAR(min_cost);
if(min_cost > INF - 1) return false;
flow += 1;
cost += round(min_cost);
assert(flow <= n);
// DEBUG_VAR(cost);
int cur = T;
int cnt_c = 0;
while(cur != S) {
int i = min_pre[cur];
cap[i]--;
cap[i ^ 1]++;
cur = ver[i ^ 1];
cnt_c++;
assert(cnt_c <= 1000);
}
return true;
}
void solve()
{
cin >> n >> m;
for(int i = 1; i <= m; i++) {
e[i].clear();
}
int sum = 0;
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i];
sum += b[i] + c[i];
int k; cin >> k;
while(k--) {
int x; cin >> x;
e[i].push_back(x);
}
}
S = n + m + 1, T = n + m + 2;
fill_n(one, T + 1, -1); idx = 0;
for(int i = 1; i <= m; i++) {
addEdge(S, i, a[i], 0);
for(int x : e[i]) {
addEdge(i, m + x, 1, 0);
}
}
for(int i = 1; i <= n; i++) {
addEdge(m + i, T, 1, 0);
}
flow = 0, cost = 0;
while(EK());
if(flow == n) {
sum -= cost/2;
cout << sum << endl;
return;
}
for(int i = 1; i <= m; i++) {
addEdge(S, i, b[i] * 2, 1);
}
while(EK());
if(flow == n) {
sum -= cost/2;
cout << sum << endl;
return;
}
for(int i = 1; i <= m; i++) {
addEdge(S, i, c[i], 2);
}
while(EK());
if(flow == n) {
sum -= cost/2;
cout << sum << endl;
return;
}
// DEBUG_HERE;
if(flow < n) {
cout << "-1" << endl;
return;
}
sum -= cost/2;
cout << sum << endl;
}
int main()
{
// freopen("1.in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T;
while(T--) {
solve();
}
return 0;
}