// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
// mylittlepony
using namespace std;
struct infoE {
int a,b,c;
bool operator<(const infoE &A) const {
return c < A.c;}
};
int main() {
// freopen("road.in","r",stdin);
int T;
scanf(" %d",&T);
for(int t =0; t < T; t++) {
int N,M;
scanf(" %d %d",&N,&M);
vector<infoE> E(M);
for(int i =0; i < M; i++) {
scanf(" %d %d %d",&E[i].a,&E[i].b,&E[i].c);
E[i].a--, E[i].b--;}
sort(E.begin(),E.end());
vector< vector<int> > comp(N);
vector<int> isC(N);
for(int i =0; i < N; i++) {
comp[i].push_back(i);
isC[i] =i;}
vector< vector< pair<int,int> > > upd(N);
vector<int> ans(N,0);
for(int i =0; i < M; i++) {
int x =isC[E[i].a], y =isC[E[i].b];
if(x == y) continue;
if(comp[x].size() < comp[y].size()) swap(x,y);
int S =0, a =upd[y].size()-1;
for(int j =comp[y].size(); j >= 0; j--) {
while(a >= 0 && upd[y][a].ff >= j) {
S +=upd[y][a].ss;
a--;}
ans[comp[y][j]] +=S;}
upd[x].push_back(make_pair(comp[x].size()-1,comp[y].size()*E[i].c));
int K =comp[x].size();
for(int j =0; j < comp[y].size(); j++) {
ans[comp[y][j]] +=K*E[i].c;
comp[x].push_back(comp[y][j]);
isC[comp[y][j]] =x;}
}
int S =0, a =upd[isC[0]].size()-1;
for(int j =comp[isC[0]].size(); j >= 0; j--) {
while(a >= 0 && upd[isC[0]][a].ff >= j) {
S +=upd[isC[0]][a].ss;
a--;}
ans[comp[isC[0]][j]] +=S;}
printf("Case %d:\n",t+1);
for(int i =0; i < N; i++) printf("%d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