#include<iostream>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include<math.h>
#include<set>
#include<string>
#include<cstring>
#include<queue>
#include<complex>
#include<sstream>
#include<cstdio>
using namespace std;
const double EPS = 1e-7 ;
const double PI = acos ( - 1.0 ) ;
const double ZERO = 0 ;
const double DOUBLE_MAX = 1 / ZERO;
typedef complex< double > point;
#define X real()
#define Y imag()
#define rad(p) (atan2((p.Y),(p.X)))
#define dot(a,b) ((conj(a)*(b)).real())
#define cross(a,b) ((conj(a)*(b)).imag())
#define length(a) hypot((a).X,(a).Y)
#define normalize(p) ((p)/length(p))
#define rotate(p,origin,ang) (((p)-(origin))*exp(point(0,(ang)))+(origin))
#define collinear(a,b,c) ( fabs( cross((a)-(b),(c)-(b))) < EPS)
#define bisector(a,b) (((a)+(b))/2.0) // return the mid point
#define polar(r,theta) (point((r),0)*exp(point(0,(theta))))
#define rep(i,n) for(int i=0;i<n;i++)
#define repi(i,a,n) for(int i=a;i<n;i++)
#define inf (0x7fffffff)
#define mp make_pair
#define read freopen("in.in","r",stdin)
#define write freopen("out.out","w",stdout)
#define scf(xx) scanf("%d",&xx)
typedef long long ll;
typedef vector< int > vi;
typedef pair< int ,int > ii;
typedef long double ld;
vector< vector< int > > comToNode;
vector< int > nodeToCom;
vector< int > indx; // order of each node in the DFS, initialized by -1
vector< int > lowLink; // after runing the algorithm, it has a unique indx for each component, this indx is the minimum indx for the set of nodes in that component
stack< int > stk; // has the current nodes currently in the stack
int curIndex;
vector< vector< ii> > adjList; // the graph
vector< bool > inStack; // 1 for nodes that are currently in the stack, 0 otherwise
void tarjanDFS( int i, int par) {
lowLink[ i] = indx[ i] = curIndex++ ;
stk.push ( i) ;
inStack[ i] = 1 ;
for ( int j= 0 ; j< adjList[ i] .size ( ) ; j++ ) {
if ( indx[ adjList[ i] [ j] .first ] == - 1 && adjList[ i] [ j] .first ! = par) {
tarjanDFS( adjList[ i] [ j] .first ,i ) ;
lowLink[ i] = min( lowLink[ i] ,lowLink[ adjList[ i] [ j] .first ] ) ;
}
else if ( inStack[ adjList[ i] [ j] .first ] && adjList[ i] [ j] .first ! = par)
lowLink[ i] = min( lowLink[ i] ,indx[ adjList[ i] [ j] .first ] ) ;
}
if ( indx[ i] == lowLink[ i] ) {
vector< int > v;
while ( ! stk.empty ( ) ) {
int ind = stk.top ( ) ;
stk.pop ( ) ;
v.push_back ( ind) ; // updating the answer
nodeToCom[ ind] = comToNode.size ( ) ; // updating the answer
inStack[ ind] = 0 ;
if ( ind== i) break ;
}
comToNode.push_back ( v) ; // updating the answer
}
}
void tarjanSCC( int n) {
indx = vector< int > ( n,- 1 ) ;
lowLink = vector< int > ( n,- 1 ) ;
nodeToCom = vector< int > ( n,- 1 ) ;
comToNode = vector< vector< int > > ( ) ;
inStack = vector< bool > ( n,0 ) ;
stk = stack< int > ( ) ;
curIndex = 0 ;
for ( int i= 0 ; i< adjList.size ( ) ; i++ )
if ( indx[ i] == - 1 )
tarjanDFS( i,- 1 ) ;
}
int n,m;
vector< vector< ii> > G;
bool vis[ 100001 ] ;
void dfs( int id) {
vis[ id] = 1 ;
rep( i,adjList[ id] .size ( ) ) {
int node= adjList[ id] [ i] .first ;
if ( ! vis[ node] ) {
if ( nodeToCom[ id] ! = nodeToCom[ node] ) {
G[ nodeToCom[ id] ] .push_back ( mp( nodeToCom[ node] ,adjList[ id] [ i] .second ) ) ;
G[ nodeToCom[ node] ] .push_back ( mp( nodeToCom[ id] ,adjList[ id] [ i] .second ) ) ;
}
dfs( node) ;
}
}
}
int parent[ 100001 ] ;
ll dis[ 100001 ] ;
int F;
ll ma;
void dfs1( int id,int par,ll d) {
if ( d> ma) { ma= d; F= id; }
dis[ id] = d; parent[ id] = par;
rep( i,G[ id] .size ( ) ) {
if ( par! = G[ id] [ i] .first )
dfs1( G[ id] [ i] .first ,id,d+ G[ id] [ i] .second ) ;
}
}
int main( ) {
//read;
//write;
int x,y,z,t; cin >> t;
while ( t-- ) {
cin >> n>> m;
adjList= vector< vector< ii> > ( n) ;
while ( m-- ) {
scanf ( "%d%d%d" ,& x,& y,& z) ;
x-- ; y-- ;
adjList[ x] .push_back ( mp( y,z) ) ;
adjList[ y] .push_back ( mp( x,z) ) ;
}
tarjanSCC( n) ;
rep( i,comToNode.size ( ) ) sort( comToNode[ i] .begin ( ) ,comToNode[ i] .end ( ) ) ;
G= vector< vector< ii> > ( comToNode.size ( ) ) ;
memset ( vis,false ,sizeof vis) ;
dfs( 0 ) ;
//memset(parent,-1,sizeof parent);
//memset(dis,-1,sizeof dis);
F= 0 ; ma= - 1 ;
dfs1( 0 ,- 1 ,0 ) ;
ma= - 1 ;
int st= F;
dfs1( F,- 1 ,0 ) ;
int en= F;
int node;
ll res= ( ll) 1 << 60 ;
ll xx;
while ( F! = - 1 ) {
xx= max( dis[ F] ,dis[ en] - dis[ F] ) ;
if ( xx< res) { res= xx; node= comToNode[ F] [ 0 ] ; }
else if ( xx== res && node > comToNode[ F] [ 0 ] ) node= comToNode[ F] [ 0 ] ;
F= parent[ F] ;
}
cout << node+ 1 << " " << res<< endl;
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPG1hcD4KI2luY2x1ZGU8Y21hdGg+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxpb21hbmlwPgojaW5jbHVkZTxjbWF0aD4KI2luY2x1ZGU8c3RhY2s+CiNpbmNsdWRlPG1hdGguaD4KI2luY2x1ZGU8c2V0PgojaW5jbHVkZTxzdHJpbmc+CiNpbmNsdWRlPGNzdHJpbmc+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTxjb21wbGV4PgkJCiNpbmNsdWRlPHNzdHJlYW0+CQojaW5jbHVkZTxjc3RkaW8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBkb3VibGUgRVBTID0gMWUtNzsKY29uc3QgZG91YmxlIFBJID0gYWNvcygtM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baV1bal0uZmlyc3QgXT09LTEgJiYgYWRqTGlzdFtpXVtqXS5maXJzdCE9cGFyKSB7CgkJCQl0YXJqYW5ERlMoIGFkakxpc3RbaV1bal0uZmlyc3QgLGkgKTsKCQkJCWxvd0xpbmtbaV0gPSBtaW4obG93TGlua1tpXSxsb3dMaW5rWyBhZGpMaXN0W2ldW2pdLmZpcnN0IF0pOwogICAgICAgICAgICAgICAgfQoJCQllbHNlIGlmKGluU3RhY2tbIGFkakxpc3RbaV1bal0uZmlyc3QgXSAmJiBhZGpMaXN0W2ldW2pdLmZpcnN0IT1wYXIpIAoJCQkJbG93TGlua1tpXSA9IG1pbihsb3dMaW5rW2ldLGluZHhbIGFkakxpc3RbaV1bal0uZmlyc3RdKTsKICAgICAgICB9CiAgICAgICAgaWYoaW5keFtpXSA9PSBsb3dMaW5rW2ldKSAgICAgICB7IAogICAgICAgICAgICAgICAgdmVjdG9yPGludD4gdjsKICAgICAgICAgICAgICAgIHdoaWxlKCFzdGsuZW1wdHkoKSkgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgaW5kID0gc3RrLnRvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICBzdGsucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHYucHVzaF9iYWNrKGluZCk7ICAgICAgICAgICAgICAgICAgICAgICAvLyB1cGRhdGluZyB0aGUgYW5zd2VyCiAgICAgICAgICAgICAgICAgICAgICAgIG5vZGVUb0NvbVtpbmRdID0gY29tVG9Ob2RlLnNpemUoKTsgICAgICAvLyB1cGRhdGluZyB0aGUgYW5zd2VyCiAgICAgICAgICAgICAgICAgICAgICAgIGluU3RhY2tbaW5kXSA9IDA7ICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICBpZihpbmQ9PWkpIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgY29tVG9Ob2RlLnB1c2hfYmFjayh2KTsgICAgICAgICAgICAgICAgICAgICAgICAgLy8gdXBkYXRpbmcgd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