#include <bits/stdc++.h>
using namespace std;
int par[105];
int ran[105];
struct edge
{
int from;
int to;
int cost;
int ind;
};
bool cmp(edge a, edge b)
{
return a.cost<b.cost;
}
void ini(int n)
{
for (int i = 0; i<n; ++i)
{
par[i] = i;
ran[i] =0;
}
}
int find_set(int i)
{
if (i==par[i])return i;
return par[i] = find_set(par[i]);
}
int join(int i, int j)
{
i = find_set(i);
j = find_set(j);
if (i!=j)
{
if (ran[i]>ran[j])
{
ran[i]+=ran[j];
par[j] = i;
}
else
{
ran[j]+=ran[i];
par[i] = j;
}
return 1;
}
return 0;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n,m;
cin>>n>>m;
ini(n);
vector<edge> v;
for (int i = 0; i<m; ++i)
{
int a,b,c;
a--;
b--;
cin>>a>>b>>c;
v.push_back({a,b,c,i});
}
sort(v.begin(),v.end(),cmp);
vector<int> ans;
int cnt = 0;
int k = 0;
int c = 0;
vector<edge> touse;
int fmin = INT_MAX;
for (int i = 0; i<v.size(); ++i)
{
if (join(v[i].from,v[i].to))
{
touse.push_back(v[i]);
cnt+=v[i].cost;
c++;
}
}
if (c==n-1)fmin=cnt;
ini(n);
cnt= 0;
c = 0;
int smin = INT_MAX;
for (int i = 0; i<touse.size(); ++i)
{
k = touse[i].ind;
for (int j = 0; j<v.size(); ++j)
{
if (v[j].ind==k)continue;
if (join(v[j].from,v[j].to))
{
cnt+=v[j].cost;
c++;
}
}
if (c!=n-1)cnt = INT_MAX;
if (cnt<smin)
{
smin = cnt;
}
ini(n);
cnt= 0;
c = 0;
}
cout<<fmin<<" "<<smin<<endl;
cout<<endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBwYXJbMTA1XTsKaW50IHJhblsxMDVdOwpzdHJ1Y3QgZWRnZQp7CiAgICBpbnQgZnJvbTsKICAgIGludCB0bzsKICAgIGludCBjb3N0OwogICAgaW50IGluZDsKfTsKYm9vbCBjbXAoZWRnZSBhLCBlZGdlIGIpCnsKICAgIHJldHVybiBhLmNvc3Q8Yi5jb3N0Owp9CnZvaWQgaW5pKGludCBuKQp7CiAgICBmb3IgKGludCBpID0gMDsgaTxuOyArK2kpCiAgICB7CiAgICAgICAgcGFyW2ldID0gaTsKICAgICAgICByYW5baV0gPTA7CiAgICB9Cn0KaW50IGZpbmRfc2V0KGludCBpKQp7CiAgICBpZiAoaT09cGFyW2ldKXJldHVybiBpOwogICAgcmV0dXJuIHBhcltpXSA9IGZpbmRfc2V0KHBhcltpXSk7Cn0KaW50IGpvaW4oaW50IGksIGludCBqKQp7CiAgICBpID0gZmluZF9zZXQoaSk7CiAgICBqID0gZmluZF9zZXQoaik7CiAgICBpZiAoaSE9aikKICAgIHsKICAgICAgICBpZiAocmFuW2ldPnJhbltqXSkKICAgICAgICB7CiAgICAgICAgICAgIHJhbltpXSs9cmFuW2pdOwogICAgICAgICAgICBwYXJbal0gPSBpOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICByYW5bal0rPXJhbltpXTsKICAgICAgICAgICAgcGFyW2ldID0gajsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICByZXR1cm4gMDsKfQppbnQgbWFpbigpCnsKICAgIGludCB0OwogICAgY2luPj50OwogICAgd2hpbGUgKHQtLSkKICAgIHsKICAgICAgICBpbnQgbixtOwogICAgICAgIGNpbj4+bj4+bTsKICAgICAgICBpbmkobik7CiAgICAgICAgdmVjdG9yPGVkZ2U+IHY7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGk8bTsgKytpKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGEsYixjOwogICAgICAgICAgICBhLS07CiAgICAgICAgICAgIGItLTsKICAgICAgICAgICAgY2luPj5hPj5iPj5jOwogICAgICAgICAgICB2LnB1c2hfYmFjayh7YSxiLGMsaX0pOwogICAgICAgIH0KICAgICAgICBzb3J0KHYuYmVnaW4oKSx2LmVuZCgpLGNtcCk7CiAgICAgICAgdmVjdG9yPGludD4gYW5zOwogICAgICAgIGludCBjbnQgPSAwOwogICAgICAgIGludCBrID0gMDsKICAgICAgICBpbnQgYyA9IDA7CiAgICAgICAgdmVjdG9yPGVkZ2U+IHRvdXNlOwogICAgICAgIGludCBmbWluID0gSU5UX01BWDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaTx2LnNpemUoKTsgKytpKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGpvaW4odltpXS5mcm9tLHZbaV0udG8pKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0b3VzZS5wdXNoX2JhY2sodltpXSk7CiAgICAgICAgICAgICAgICBjbnQrPXZbaV0uY29zdDsKICAgICAgICAgICAgICAgIGMrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoYz09bi0xKWZtaW49Y250OwogICAgICAgIGluaShuKTsKICAgICAgICBjbnQ9IDA7CiAgICAgICAgYyA9IDA7CiAgICAgICAgaW50IHNtaW4gPSBJTlRfTUFYOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpPHRvdXNlLnNpemUoKTsgKytpKQogICAgICAgIHsKICAgICAgICAgICAgayA9IHRvdXNlW2ldLmluZDsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGo8di5zaXplKCk7ICsraikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKHZbal0uaW5kPT1rKWNvbnRpbnVlOwogICAgICAgICAgICAgICAgaWYgKGpvaW4odltqXS5mcm9tLHZbal0udG8pKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGNudCs9dltqXS5jb3N0OwogICAgICAgICAgICAgICAgICAgIGMrKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoYyE9bi0xKWNudCA9IElOVF9NQVg7CiAgICAgICAgICAgIGlmIChjbnQ8c21pbikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc21pbiA9IGNudDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpbmkobik7CiAgICAgICAgICAgIGNudD0gMDsKICAgICAgICAgICAgYyA9IDA7CiAgICAgICAgfQogICAgICAgIGNvdXQ8PGZtaW48PCIgIjw8c21pbjw8ZW5kbDsKICAgICAgICBjb3V0PDxlbmRsOwogICAgfQp9Cg==