#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int >ii;
typedef long long ll;
vector<pair<int,ii> >g;
vector<int>parent;
vector<int>rnk;
vector<ii>mst;
ll tot_wt;
int find_(int u){
if(u!=parent[u])
parent[u]=find_(parent[u]);
return parent[u];
}
void _union(int x,int y){
int xset=find_(x);
int yset=find_(y);
if(rnk[xset]>rnk[yset])
parent[yset]=xset;
if(rnk[yset]>rnk[xset])
parent[xset]=yset;
else{
parent[yset]=xset;
rnk[xset]++;
}
}
ll kruskal(ii k){
tot_wt=0;
for(int i=0;i<g.size();i++){
int u=g[i].second.first,v=g[i].second.second;
if((u==k.first)&& (v==k.second)){
//cout<<u<<" "<<v<<"\n";
continue;
}
int uset=find_(u),vset=find_(v);
if(uset!=vset){
tot_wt+=g[i].first;
_union(uset,vset);
//cout<<u<<" "<<v<<"\n";
if(k.first==-1&&k.second==-1)
mst.push_back({u,v});
}
}
return tot_wt;
}
int main(){
int t;cin>>t;
while(t--){
int n,m;cin>>n>>m;
g.assign(m,pair<int,ii>());
parent.assign(n+1,0);
rnk.assign(n+1,0);
for(int i=0;i<m;i++){
int a,b,w;cin>>a>>b>>w;
g[i]={w,{a,b}};
}
sort(g.begin(),g.end());
for(int i=1;i<=n;i++)
parent[i]=i;
cout<<kruskal({-1,-1})<<" ";
ll swt=INT_MAX;
for(int i=0;i<mst.size();i++){
//cout<<mst[i].first<<" "<<mst[i].second<<"\n";
for(int i=0;i<=n;i++)
parent[i]=i;
for(int i=0;i<=n;i++)
rnk[i]=0;
swt=min(swt,kruskal(mst[i]));
}
cout<<swt<<"\n";}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBwYWlyPGludCxpbnQgPmlpOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdmVjdG9yPHBhaXI8aW50LGlpPiA+ZzsKdmVjdG9yPGludD5wYXJlbnQ7CnZlY3RvcjxpbnQ+cm5rOwp2Z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JhY2soe3Usdn0pOwogICAgICAgIH0KICAgIH0KCnJldHVybiB0b3Rfd3Q7Cn0KCmludCBtYWluKCl7CiAgICBpbnQgdDtjaW4+PnQ7CiAgICB3aGlsZSh0LS0pewogICAgaW50IG4sbTtjaW4+Pm4+Pm07CiAgICBnLmFzc2lnbihtLHBhaXI8aW50LGlpPigpKTsKICAgIHBhcmVudC5hc3NpZ24obisxLDApOwogICAgcm5rLmFzc2lnbihuKzEsMCk7CiAgICBmb3IoaW50IGk9MDtpPG07aSsrKXsKICAgICAgICBpbnQgYSxiLHc7Y2luPj5hPj5iPj53OwogICAgICAgIGdbaV09e3cse2EsYn19OwogICAgfQogICAgc29ydChnLmJlZ2luKCksZy5lbmQoKSk7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKykKICAgICAgICBwYXJlbnRbaV09aTsKICAgIGNvdXQ8PGtydXNrYWwoey0xLC0xfSk8PCIgIjsKCgoKICAgIGxsIHN3dD1JTlRfTUFYOwogICAgZm9yKGludCBpPTA7aTxtc3Quc2l6ZSgpO2krKyl7CiAgICAgICAgLy9jb3V0PDxtc3RbaV0uZmlyc3Q8PCIgIjw8bXN0W2ldLnNlY29uZDw8IlxuIjsKICAgICAgICBmb3IoaW50IGk9MDtpPD1uO2krKykKICAgICAgICBwYXJlbnRbaV09aTsKICAgIGZvcihpbnQgaT0wO2k8PW47aSsrKQogICAgICAgIHJua1tpXT0wOwogICAgICAgIHN3dD1taW4oc3d0LGtydXNrYWwobXN0W2ldKSk7CgogICAgfQogICAgY291dDw8c3d0PDwiXG4iO30KCn0K