package Week2;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
/*温迪有一个国家,他想建立一支军队来保护他的国家。他接了N个女孩和M个男孩,想把他们收起来当他的士兵。征集一个没有任何特权的士兵,
必须交1万元。女孩和男孩之间有一些关系,Windy可以利用这些关系来降低成本。如果女孩x和男孩y有关系d,其中一个已经被收走,
Windy可以用10000-d人民币来收另一个。
现在考虑到女孩和男孩之间的所有关系,你的任务是找到温迪必须支付的最少的钱。请注意,收集一个士兵时只能使用一个关系。
输入的第一行是测试用例的数量。
每个测试用例的第一行包含三个整数N、M和R。
然后R行,每个包含三个整数Xi,Yi和Di。
每个测试用例前都有一个空行。
对于每个测试用例,在一行中输出答案。*/
//71071
//54223
//2
//
//5 5 8
//4 3 6831
//1 3 4583
//0 0 6592
//0 1 3063
//3 3 4975
//1 3 2049
//4 2 2104
//2 2 781
//
//5 5 10
//2 4 9820
//3 2 6236
//3 1 8864
//2 4 8326
//2 0 5156
//2 0 1463
//4 1 2439
//0 4 4373
//3 4 8889
//2 4 3133
public class I_Kruskal {
static int T;
static int N,M,R;
static NodeI nodes[];
static List<NodeI> adjList;
static int parent[];
static int ans;
static int costMax;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("Solution.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
T = Integer.parseInt(st.nextToken());
for (int t = 1; t <= T; t++) {
st = new StringTokenizer(br.readLine());
if(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
parent = new int[N+M+2];
nodes = new NodeI[R+2];
//N个女孩,M个男孩总人数N+M个人,case坐标M人的时候变换一下,加个N值即不重复,每个坐标不重复即可以用并查集
for (int i = 0; i <= N+M-1; i++) {
parent[i] = i;
}
for (int i = 0; i < R; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
nodes[i]=new NodeI(s,N+e,d);
}
Arrays.sort(nodes, 0, R, new Comparator<NodeI>() {
@Override
public int compare(NodeI o1, NodeI o2) {
return o2.d-o1.d;
}
});;
ans = 0;
costMax = 0;
for (int i = 0; i < R; i++) {
if(find(nodes[i].s)!=find(nodes[i].e)) {
union(nodes[i].s,nodes[i].e);
costMax = costMax+nodes[i].d;
}
}
ans = (N+M)*10000-costMax;//雇佣N+M个员工,总共10000*(M+N),通过雇佣关系减去D可以省的钱
System.out.println(ans);
}
}
public static void union(int a,int b) {
int x = find(a);
int y = find(b);
if(x==y) {
return;
}
if(x<y) {
parent[y]=x;
}
if(x>y) {
parent[x]=y;
}
}
public static int find(int i) {
if(i==parent[i]) {
return i;
}
return parent[i]=find(parent[i]);
}
}
class NodeI {
int s;
int e;
int d;
public NodeI(int s,int e,int d) {
this.s=s;
this.e = e;
this.d = d;
}
}