#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int n, m;
struct ***
{
int from, to, len;
}ed[1000005];
int id[1005];
int pre[1005];
int vis[1005];
int in[1005];
int zhuliu(int root)
{
int res = 0;
while (1)
{
for (int s = 0; s < n; s++)
in[s] = inf;
for (int s = 0; s < m; s++)
{
if (ed[s].from != ed[s].to&&in[ed[s].to] > ed[s].len)
{
in[ed[s].to] = ed[s].len;
pre[ed[s].to] = ed[s].from;
}
}
for (int s = 0; s < n; s++)
if (in[s] == inf && s != root)
return -1;
int sum_huan = 0;
in[root] = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
for (int s = 0; s < n; s++)
{
res += in[s];
int v = s;
while (vis[v] != s&&id[v] == -1 && v != root)
{
vis[v] = s;
v = pre[v];
}
if (v != root&&id[v] == -1)
{
for (int w = pre[v]; w != v; w = pre[w])
id[w] = sum_huan;
id[v] = sum_huan++;
}
}
if (sum_huan == 0)
break;
for (int s = 0; s < n; s++)
{
if (id[s] == -1)
id[s] = sum_huan++;
}
for (int s = 0; s < m; s++)
{
int v = ed[s].to;
ed[s].from = id[ed[s].from];
ed[s].to = id[ed[s].to];
if (ed[s].from != ed[s].to)
{
ed[s].len -= in[v];
}
}
n = sum_huan;
root = id[root];
}
return res;
}
int main()
{
int te, cas = 1;
scanf("%d", &te);
while (te--)
{
scanf("%d", &n);
scanf("%d", &m);
for (int s = 0; s < m; s++)
{
scanf("%d%d%d", &ed[s].from, &ed[s].to, &ed[s].len);
if (ed[s].from == ed[s].to)
{
ed[s].len = inf;
}
}
int ans = zhuliu(0);
if (ans == -1)
{
printf("Case #%d: 无成图!\n", cas++);
}
else
{
printf("Case #%d: %d\n", cas++, ans);
}
}
}