#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pi pair<int,int>
#define adjs(name, type, size) vector<vector<type>>name(size)
#define adjpass(name, type) vector<vector<type>>&name
#define killua ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
int cases = 1;
int timer = 1;
set<pi > bridges;
void dfsSCC(int node, int dad, vector<int> &dfn, vector<int> &lowlink, adjpass(v, int)) {
dfn[node] = lowlink[node] = timer++;
for (auto i: v[node]) {
if (i == dad)
continue;
if (dfn[i] == -1) { //not vis
dfsSCC(i, node, dfn, lowlink, v);
lowlink[node] = min(lowlink[node], lowlink[i]);
if (lowlink[i] > dfn[node]) {
bridges.insert({min(node, i), max(node, i)});
}
} else {
lowlink[node] = min(lowlink[node], dfn[i]);
}
}
}
void dfs(int node, vector<set<pi >> &v, vector<int> &vis, int &last, int &mx, int cur) {
vis[node] = 1;
if (cur > mx) {
mx = cur;
last = min(last, node);
last = node;
} else if (cur == mx) {
last = min(last, node);
}
for (auto i: v[node]) {
if (!vis[i.first]) {
dfs(i.first, v, vis, last, mx, cur + i.second);
}
}
}
class DSU {
private:
vector<int> rank, par, size;
public:
DSU(int n = 0) {
rank.resize(n + 1);
par.resize(n + 1);
size.resize(n + 1, 1);
for (int i = 0; i <= n; i++)
par[i] = i;
}
int findUpar(int node) {
if (node == par[node])
return node;
return par[node] = findUpar(par[node]);//path comp
}
void unionbyRank(int u, int v) {
int UparU = findUpar(u);
int UparV = findUpar(v);
if (UparU == UparV)
return;
if (rank[UparU] < rank[UparV]) {
par[UparU] = UparV;
} else {
par[UparV] = UparU;
rank[UparU] += (rank[UparU] == rank[UparV]);
}
}
void unionbySize(int u, int v) {
int UparU = findUpar(u);
int UparV = findUpar(v);
if (UparU == UparV)
return;
if (size[UparU] < size[UparV]) {
par[UparU] = UparV;
size[UparV] += size[UparU];
} else {
par[UparV] = UparU;
size[UparU] += size[UparV];
}
}
};
void gon() {
int n, m;
cin >> n >> m;
adjs(v, int, n + 5);
vector<pair<pi, int> > ed;
for (int i = 0; i < m; i++) {
int l, r, c;
cin >> l >> r >> c;
ed.push_back({{min(l, r), max(l, r)}, c});
v[l].push_back(r);
v[r].push_back(l);
}
vector<int> dfn(n + 5, -1), lowlink(n + 5, -1);
timer = 1;
bridges.clear();
for (int i = 1; i <= n; i++) {
if (dfn[i] == -1) {
dfsSCC(i, -1, dfn, lowlink, v);
}
}
map<pi, int> mp;
for (auto i: bridges)
mp[i] = 1;
for (auto &i: ed) {
if (!mp.count(i.first))
i.second = 0;
}
DSU d(n + 5);
for (auto i: ed) {
if (i.second == 0) {
d.unionbySize(i.first.first, i.first.second);
}
}
vector<int> comp(n + 5, 1e9);
for (int i = 1; i <= n; i++) {
comp[d.findUpar(i)] = min(comp[d.findUpar(i)], i);
}
vector<set<pi >> mst(n + 5);
for (auto i: ed) {
if (i.second == 0) {
continue;
}
int a = comp[d.findUpar(i.first.first)];
int b = comp[d.findUpar(i.first.second)];
mst[a].insert({b, i.second});
mst[b].insert({a, i.second});
}
int St = 1;
int last = 1;
int mx = 0;
int cur = 0;
vector<int> vis(n + 5);
dfs(St, mst, vis, last, mx, cur);
vis.assign(n + 5, 0);
int L = last;
St = last;
mx = 0;
cur = 0;
dfs(St, mst, vis, last, mx, cur);
int R = last;
vector<pi > dad(n + 5, {-1, -1});
vector<int> dis(n + 5, 1e18);
dis[last] = 0;
priority_queue<pi > pq;
pq.push({0, -last});
while (pq.size()) {
auto [cost, node] = pq.top();
node *= -1;
pq.pop();
if (cost > dis[node])
continue;
for (auto i: mst[node]) {
if (cost + i.second < dis[i.first]) {
dad[i.first] = {node, i.second};
dis[i.first] = cost + i.second;
pq.push({dis[i.first], -i.first});
}
}
}
int Node = L;
vector<int> path;
vector<int> vals;
vals.push_back(0);
int sum = 0;
while (dad[L].first != -1) {
sum += dad[L].second;
vals.push_back(dad[L].second);
path.push_back(L);
L = dad[L].first;
}
path.push_back(L);
int ansNode = path[0], mxcost = sum, pre = 0;
for (int i = 0; i < vals.size(); i++) {
pre += vals[i];
sum -= vals[i];
int mxcur = max(pre, sum);
if (mxcur < mxcost) {
mxcost = mxcur;
ansNode = path[i];
} else if (mxcur == mxcost) {
ansNode = min(ansNode, path[i]);
}
}
cout << ansNode << " " << mxcost << endl;
}
int32_t main() {
killua;
int t = 1;
if (cases) cin >> t;
while (t--) {
gon();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBlbmRsICdcbicKI2RlZmluZSBwaSBwYWlyPGludCxpbnQ+CiNkZWZpbmUgYWRqcyhuYW1lLCB0eXBlLCBzaXplKSB2ZWN0b3I8dmVjdG9yPHR5cGU+Pm5hbWUoc2l6ZSkKI2RlZmluZSBhZGpwYXNzKG5hbWUsIHR5cGUpIHZlY3Rvcjx2ZWN0b3I8dHlwZT4+Jm5hbWUKI2RlZmluZSBraWxsdWEgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSksIGNpbi50aWUoTlVMTCksIGNvdXQudGllKDApCmludCBjYXNlcyA9IDE7CgppbnQgdGltZXIgPSAxOwpzZXQ8cGkgPiBicmlkZ2VzOwoKdm9pZCBkZnNTQ0MoaW50IG5vZGUsIGludCBkYWQsIHZlY3RvcjxpbnQ+ICZkZm4sIHZlY3RvcjxpbnQ+ICZsb3dsaW5rLCBhZGpwYXNzKHYsIGludCkpIHsKICAgIGRmbltub2RlXSA9IGxvd2xpbmtbbm9kZV0gPSB0aW1lcisrOwogICAgZm9yIChhdXRvIGk6IHZbbm9kZV0pIHsKICAgICAgICBpZiAoaSA9PSBkYWQpCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIGlmIChkZm5baV0gPT0gLTEpIHsgLy9ub3QgdmlzCiAgICAgICAgICAgIGRmc1NDQyhpLCBub2RlLCBkZm4sIGxvd2xpbmssIHYpOwogICAgICAgICAgICBsb3dsaW5rW25vZGVdID0gbWluKGxvd2xpbmtbbm9kZV0sIGxvd2xpbmtbaV0pOwogICAgICAgICAgICBpZiAobG93bGlua1tpXSA+IGRmbltub2RlXSkgewogICAgICAgICAgICAgICAgYnJpZGdlcy5pbnNlcnQoe21pbihub2RlLCBpKSwgbWF4KG5vZGUsIGkpfSk7CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBsb3dsaW5rW25vZGVdID0gbWluKGxvd2xpbmtbbm9kZV0sIGRmbltpXSk7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIGRmcyhpbnQgbm9kZSwgdmVjdG9yPHNldDxwaSA+PiAmdiwgdmVjdG9yPGludD4gJnZpcywgaW50ICZsYXN0LCBpbnQgJm14LCBpbnQgY3VyKSB7CiAgICB2aXNbbm9kZV0gPSAxOwogICAgaWYgKGN1ciA+IG14KSB7CiAgICAgICAgbXggPSBjdXI7CiAgICAgICAgbGFzdCA9IG1pbihsYXN0LCBub2RlKTsKICAgICAgICBsYXN0ID0gbm9kZTsKICAgIH0gZWxzZSBpZiAoY3VyID09IG14KSB7CiAgICAgICAgbGFzdCA9IG1pbihsYXN0LCBub2RlKTsKICAgIH0KICAgIGZvciAoYXV0byBpOiB2W25vZGVdKSB7CiAgICAgICAgaWYgKCF2aXNbaS5maXJzdF0pIHsKICAgICAgICAgICAgZGZzKGkuZmlyc3QsIHYsIHZpcywgbGFzdCwgbXgsIGN1ciArIGkuc2Vjb25kKTsKICAgICAgICB9CiAgICB9Cn0KCmNsYXNzIERTVSB7CnByaXZhdGU6CiAgICB2ZWN0b3I8aW50PiByYW5rLCBwYXIsIHNpemU7CnB1YmxpYzoKICAgIERTVShpbnQgbiA9IDApIHsKICAgICAgICByYW5rLnJlc2l6ZShuICsgMSk7CiAgICAgICAgcGFyLnJlc2l6ZShuICsgMSk7CiAgICAgICAgc2l6ZS5yZXNpemUobiArIDEsIDEpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IG47IGkrKykKICAgICAgICAgICAgcGFyW2ldID0gaTsKICAgIH0KCiAgICBpbnQgZmluZFVwYXIoaW50IG5vZGUpIHsKICAgICAgICBpZiAobm9kZSA9PSBwYXJbbm9kZV0pCiAgICAgICAgICAgIHJldHVybiBub2RlOwogICAgICAgIHJldHVybiBwYXJbbm9kZV0gPSBmaW5kVXBhcihwYXJbbm9kZV0pOy8vcGF0aCBjb21wCiAgICB9CgogICAgdm9pZCB1bmlvbmJ5UmFuayhpbnQgdSwgaW50IHYpIHsKICAgICAgICBpbnQgVXBhclUgPSBmaW5kVXBhcih1KTsKICAgICAgICBpbnQgVXBhclYgPSBmaW5kVXBhcih2KTsKICAgICAgICBpZiAoVXBhclUgPT0gVXBhclYpCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICBpZiAocmFua1tVcGFyVV0gPCByYW5rW1VwYXJWXSkgewogICAgICAgICAgICBwYXJbVXBhclVdID0gVXBhclY7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcGFyW1VwYXJWXSA9IFVwYXJVOwogICAgICAgICAgICByYW5rW1VwYXJVXSArPSAocmFua1tVcGFyVV0gPT0gcmFua1tVcGFyVl0pOwogICAgICAgIH0KICAgIH0KCiAgICB2b2lkIHVuaW9uYnlTaXplKGludCB1LCBpbnQgdikgewogICAgICAgIGludCBVcGFyVSA9IGZpbmRVcGFyKHUpOwogICAgICAgIGludCBVcGFyViA9IGZpbmRVcGFyKHYpOwogICAgICAgIGlmIChVcGFyVSA9PSBVcGFyVikKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIGlmIChzaXplW1VwYXJVXSA8IHNpemVbVXBhclZdKSB7CiAgICAgICAgICAgIHBhcltVcGFyVV0gPSBVcGFyVjsKICAgICAgICAgICAgc2l6ZVtVcGFyVl0gKz0gc2l6ZVtVcGFyVV07CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcGFyW1VwYXJWXSA9IFVwYXJVOwogICAgICAgICAgICBzaXplW1VwYXJVXSArPSBzaXplW1VwYXJWXTsKICAgICAgICB9CiAgICB9Cn07Cgp2b2lkIGdvbigpIHsKICAgIGludCBuLCBtOwogICAgY2luID4+IG4gPj4gbTsKICAgIGFkanModiwgaW50LCBuICsgNSk7CiAgICB2ZWN0b3I8cGFpcjxwaSwgaW50PiA+IGVkOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICBpbnQgbCwgciwgYzsKICAgICAgICBjaW4gPj4gbCA+PiByID4+IGM7CiAgICAgICAgZWQucHVzaF9iYWNrKHt7bWluKGwsIHIpLCBtYXgobCwgcil9LCBjfSk7CiAgICAgICAgdltsXS5wdXNoX2JhY2socik7CiAgICAgICAgdltyXS5wdXNoX2JhY2sobCk7CiAgICB9CiAgICB2ZWN0b3I8aW50PiBkZm4obiArIDUsIC0xKSwgbG93bGluayhuICsgNSwgLTEpOwogICAgdGltZXIgPSAxOwogICAgYnJpZGdlcy5jbGVhcigpOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgaWYgKGRmbltpXSA9PSAtMSkgewogICAgICAgICAgICBkZnNTQ0MoaSwgLTEsIGRmbiwgbG93bGluaywgdik7CiAgICAgICAgfQogICAgfQogICAgbWFwPHBpLCBpbnQ+IG1wOwogICAgZm9yIChhdXRvIGk6IGJyaWRnZXMpCiAgICAgICAgbXBbaV0gPSAxOwogICAgZm9yIChhdXRvICZpOiBlZCkgewogICAgICAgIGlmICghbXAuY291bnQoaS5maXJzdCkpCiAgICAgICAgICAgIGkuc2Vjb25kID0gMDsKICAgIH0KICAgIERTVSBkKG4gKyA1KTsKICAgIGZvciAoYXV0byBpOiBlZCkgewogICAgICAgIGlmIChpLnNlY29uZCA9PSAwKSB7CiAgICAgICAgICAgIGQudW5pb25ieVNpemUoaS5maXJzdC5maXJzdCwgaS5maXJzdC5zZWNvbmQpOwogICAgICAgIH0KICAgIH0KICAgIHZlY3RvcjxpbnQ+IGNvbXAobiArIDUsIDFlOSk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBjb21wW2QuZmluZFVwYXIoaSldID0gbWluKGNvbXBbZC5maW5kVXBhcihpKV0sIGkpOwogICAgfQogICAgdmVjdG9yPHNldDxwaSA+PiBtc3QobiArIDUpOwogICAgZm9yIChhdXRvIGk6IGVkKSB7CiAgICAgICAgaWYgKGkuc2Vjb25kID09IDApIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGludCBhID0gY29tcFtkLmZpbmRVcGFyKGkuZmlyc3QuZmlyc3QpXTsKICAgICAgICBpbnQgYiA9IGNvbXBbZC5maW5kVXBhcihpLmZpcnN0LnNlY29uZCldOwogICAgICAgIG1zdFthXS5pbnNlcnQoe2IsIGkuc2Vjb25kfSk7CiAgICAgICAgbXN0W2JdLmluc2VydCh7YSwgaS5zZWNvbmR9KTsKICAgIH0KICAgIGludCBTdCA9IDE7CiAgICBpbnQgbGFzdCA9IDE7CiAgICBpbnQgbXggPSAwOwogICAgaW50IGN1ciA9IDA7CiAgICB2ZWN0b3I8aW50PiB2aXMobiArIDUpOwogICAgZGZzKFN0LCBtc3QsIHZpcywgbGFzdCwgbXgsIGN1cik7CiAgICB2aXMuYXNzaWduKG4gKyA1LCAwKTsKICAgIGludCBMID0gbGFzdDsKICAgIFN0ID0gbGFzdDsKICAgIG14ID0gMDsKICAgIGN1ciA9IDA7CiAgICBkZnMoU3QsIG1zdCwgdmlzLCBsYXN0LCBteCwgY3VyKTsKICAgIGludCBSID0gbGFzdDsKICAgIHZlY3RvcjxwaSA+IGRhZChuICsgNSwgey0xLCAtMX0pOwogICAgdmVjdG9yPGludD4gZGlzKG4gKyA1LCAxZTE4KTsKICAgIGRpc1tsYXN0XSA9IDA7CiAgICBwcmlvcml0eV9xdWV1ZTxwaSA+IHBxOwogICAgcHEucHVzaCh7MCwgLWxhc3R9KTsKICAgIHdoaWxlIChwcS5zaXplKCkpIHsKICAgICAgICBhdXRvIFtjb3N0LCBub2RlXSA9IHBxLnRvcCgpOwogICAgICAgIG5vZGUgKj0gLTE7CiAgICAgICAgcHEucG9wKCk7CiAgICAgICAgaWYgKGNvc3QgPiBkaXNbbm9kZV0pCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIGZvciAoYXV0byBpOiBtc3Rbbm9kZV0pIHsKICAgICAgICAgICAgaWYgKGNvc3QgKyBpLnNlY29uZCA8IGRpc1tpLmZpcnN0XSkgewogICAgICAgICAgICAgICAgZGFkW2kuZmlyc3RdID0ge25vZGUsIGkuc2Vjb25kfTsKICAgICAgICAgICAgICAgIGRpc1tpLmZpcnN0XSA9IGNvc3QgKyBpLnNlY29uZDsKICAgICAgICAgICAgICAgIHBxLnB1c2goe2Rpc1tpLmZpcnN0XSwgLWkuZmlyc3R9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGludCBOb2RlID0gTDsKICAgIHZlY3RvcjxpbnQ+IHBhdGg7CiAgICB2ZWN0b3I8aW50PiB2YWxzOwogICAgdmFscy5wdXNoX2JhY2soMCk7CiAgICBpbnQgc3VtID0gMDsKICAgIHdoaWxlIChkYWRbTF0uZmlyc3QgIT0gLTEpIHsKICAgICAgICBzdW0gKz0gZGFkW0xdLnNlY29uZDsKICAgICAgICB2YWxzLnB1c2hfYmFjayhkYWRbTF0uc2Vjb25kKTsKICAgICAgICBwYXRoLnB1c2hfYmFjayhMKTsKICAgICAgICBMID0gZGFkW0xdLmZpcnN0OwogICAgfQogICAgcGF0aC5wdXNoX2JhY2soTCk7CiAgICBpbnQgYW5zTm9kZSA9IHBhdGhbMF0sIG14Y29zdCA9IHN1bSwgcHJlID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdmFscy5zaXplKCk7IGkrKykgewogICAgICAgIHByZSArPSB2YWxzW2ldOwogICAgICAgIHN1bSAtPSB2YWxzW2ldOwogICAgICAgIGludCBteGN1ciA9IG1heChwcmUsIHN1bSk7CiAgICAgICAgaWYgKG14Y3VyIDwgbXhjb3N0KSB7CiAgICAgICAgICAgIG14Y29zdCA9IG14Y3VyOwogICAgICAgICAgICBhbnNOb2RlID0gcGF0aFtpXTsKICAgICAgICB9IGVsc2UgaWYgKG14Y3VyID09IG14Y29zdCkgewogICAgICAgICAgICBhbnNOb2RlID0gbWluKGFuc05vZGUsIHBhdGhbaV0pOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgYW5zTm9kZSA8PCAiICIgPDwgbXhjb3N0IDw8IGVuZGw7Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIGtpbGx1YTsKICAgIGludCB0ID0gMTsKICAgIGlmIChjYXNlcykgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgZ29uKCk7CiAgICB9Cn0=