#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 20005;
const int Mod = 1000000007;
int n,m,R,ans;
int p[maxn];
struct node
{
int x,y,w;
}edge[maxn*3];
bool cmp( node a,node b )
{
return a.w > b.w;
}
int find( int x )
{
return p[x] == x?x:p[x] = find( p[x] );
}
void merge( int i )
{
int x = find( edge[i].x );
int y = find( edge[i].y );
if( x != y )
{
p[x] = y;
ans += edge[i].w;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
ans = 0;
scanf("%d%d%d",&n,&m,&R);
for( int i = 0; i <= n+m; i ++ ) p[i] = i;
for( int i = 0; i < R; i ++ )
{
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
edge[i].y += n;
}
sort( edge,edge+R,cmp );
for( int i = 0; i < R; i ++ )
merge( i );
printf("%d\n",(n+m)*10000-ans);
}
return 0;
}