C++#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 1e3 + 50;
int n,q;
int in[9][N],w[9],p[N],X[N],Y[N];
struct Edge
{
int x,y,z;
}e[N * N];
const bool cmp(Edge x,Edge y) { return x.z < y.z; }
int get_p(int x) { return p[x] == x ? x : p[x] = get_p(p[x]); }
void solve()
{
read(n); read(q);
for(int i = 1;i <= q; ++ i)
{
read(in[i][0]); read(w[i]);
for(int j = 1;j <= in[i][0]; ++ j)
read(in[i][j]);
}
int m = 0;
for(int i = 1;i <= n; ++ i)
{
read(X[i]); read(Y[i]);
for(int j = 1;j < i; ++ j)
{
e[++ m].x = i;
e[m].y = j;
e[m].z = (X[i] - X[j]) * (X[i] - X[j]) + (Y[i] - Y[j]) * (Y[i] - Y[j]);
}
}
sort(e + 1,e + 1 + m,cmp);
int res = 0,ans = 0;
for(int i = 1;i <= n; ++ i) p[i] = i;
for(int i = 1;i <= m; ++ i)
{
int x = e[i].x, y = e[i].y;
x = get_p(e[i].x),y = get_p(e[i].y);
if(x != y)
{
e[++ res] = e[i];
p[x] = y;
ans += e[i].z;
}
}
int s = (1 << q) - 1;
for(int i = 1;i <= s;++ i)
{
int ans2 = 0;
for(int j = 1;j <= n; ++ j) p[j] = j;
for(int j = 1;j <= 8; ++ j)
if(i&(1 << j - 1))
{
ans2 += w[j];
for(int k = 2;k <= in[j][0]; ++ k)
{
int x = in[j][k],y = in[j][k - 1];
x = get_p(x);y = get_p(y);
if(x != y) p[x] = y;
}
}
for(int j = 1;j < n; ++ j)
{
int x = e[j].x, y = e[j].y;
x = get_p(e[j].x),y = get_p(e[j].y);
if(x != y)
{
p[x] = y;
ans2 += e[j].z;
}
}
ans = min(ans,ans2);
}
cout << ans << endl;
}
int main()
{
int t;
read(t);
while(t --)
{
solve();
if(t) cout << endl;
}
return 0;
}