In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  

Input

The first line contains the number of test cases. 
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities. 
To make it easy, the cities are signed from 1 to n. 
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q. 
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities. 

Output

For each case, output the least money you need to take, if it’s impossible, just output -1.

Sample Input

1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6

Sample Output

1

C++版本一

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int N=510;
int T,t,m,n,k,pre[N],id[N];
struct node{
    int f,t,w;
    node(){};
    node(int a,int b,int c){
        f=a;
        t=b;
        w=c;
    }
    bool operator < (const node &S )const{

        return w<S.w;
    }
}e[N*N];
int find(int x){
    int r=x;
    while(pre[r]!=r)
        r=pre[r];
    return r;
}
void join(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        pre[fx]=fy;
}
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        int a,b,c;
        int cnt=0;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            e[++cnt]=node(a,b,c);
        }
        sort(e+1,e+cnt+1);
        for(int i=1;i<=n;i++)
            pre[i]=i;
        for(int K=1;K<=k;K++){
            scanf("%d",&t);
            for(int i=1;i<=t;i++){
                scanf("%d",&id[i]);
                for(int j=1;j<i;j++){
                    join(id[i],id[j]);
                }
            }
        }
        int ans=0;
        for(int i=1;i<=cnt;i++)
            if(find(e[i].f)!=find(e[i].t)){
                join(e[i].f,e[i].t);
                ans+=e[i].w;
            }
        int flag=0;
        for(int i=1;i<=n;i++)
            if (pre[i]==i)
                flag++;
        if(flag==1)
            cout << ans << endl;
        else
            cout << -1 << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 25003;

typedef struct Road{
	int x;
	int y;
	int c;
}Road;

Road road[MAX];
int cr;

int pre[501];

void init(int n){
	int i;
	for(i=1;i<=n;++i){
		pre[i] = i;
	}
	cr = n-1;
}

int root(int x){
	if(x!=pre[x]){
		pre[x] = root(pre[x]);
	}
	return pre[x];
}

int merge(int x,int y){
	if(x==-1)return 0;
	int ret = 0;
	int fx = root(x);
	int fy = root(y);
	if(fx!=fy){
		--cr;
		pre[fx] = fy;
		ret = 1;
	}
	return ret;
}

int cmp(const void *a,const void *b){
	return ((Road *)a)->c - ((Road *)b)->c;
}

int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t,n,m,k,cid,tc,pre;
	int sum,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d %d",&n,&m,&k);
		init(n);
		for(i=0;i<m;++i){
			scanf("%d %d %d",&road[i].x,&road[i].y,&road[i].c);
		}
		sum = 0;
		for(i=0;i<k;++i){
			scanf("%d",&tc);
			pre = -1;
			for(j=0;j<tc;++j){
				scanf("%d",&cid);
				merge(pre,cid);
				pre = cid;
			}
		}
		qsort(road,m,sizeof(Road),cmp);
		for(i=0;i<m;++i){
			if(merge(road[i].x,road[i].y)==1){
				sum += road[i].c;
			}
		}
		if(cr!=0){
			printf("-1\n");
		}else{
			printf("%d\n",sum);
		}
	}
    return 0;
}

C++版本三

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 25003;

typedef struct Road{
	int x;
	int y;
	int c;
}Road;

Road road[MAX];
int cr;

int pre[501];

void init(int n){
	int i;
	for(i=1;i<=n;++i){
		pre[i] = i;
	}
	cr = n-1;
}

int root(int x){
	if(x!=pre[x]){
		pre[x] = root(pre[x]);
	}
	return pre[x];
}

int merge(int x,int y){
	if(x==-1)return 0;
	int ret = 0;
	int fx = root(x);
	int fy = root(y);
	if(fx!=fy){
		--cr;
		pre[fx] = fy;
		ret = 1;
	}
	return ret;
}

int cmp(const void *a,const void *b){
	return ((Road *)a)->c - ((Road *)b)->c;
}

int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t,n,m,k,cid,tc,pre;
	int sum,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d %d",&n,&m,&k);
		init(n);
		for(i=0;i<m;++i){
			scanf("%d %d %d",&road[i].x,&road[i].y,&road[i].c);
		}
		sum = 0;
		for(i=0;i<k;++i){
			scanf("%d",&tc);
			pre = -1;
			for(j=0;j<tc;++j){
				scanf("%d",&cid);
				merge(pre,cid);
				pre = cid;
			}
		}
		qsort(road,m,sizeof(Road),cmp);
		for(i=0;i<m;++i){
			if(merge(road[i].x,road[i].y)==1){
				sum += road[i].c;
			}
		}
		if(cr!=0){
			printf("-1\n");
		}else{
			printf("%d\n",sum);
		}
	}
    return 0;
}