#include<bits/stdc++.h>
using namespace std;
#ifdef ANI
#include "D:/DUSTBIN/local_inc.h"
#else
#define dbg(...) 0
#endif
class LCA{
public:
int MB=20,N,ROOT;
vector<vector<int>> up,e;
vector<int> depth;
void dfs(int cur,int par) {
up[cur][0]=par;
for(int bit=1;bit<=MB;bit++) {
up[cur][bit]=up[up[cur][bit-1]][bit-1];
}
for(int node:e[cur]) {
if(node!=par) {
depth[node]=depth[cur]+1;
dfs(node,cur);
}
}
}
LCA(vector<vector<int>> _,int root=0){
e=_; N=e.size(); ROOT=root;
up=vector<vector<int>>(N,vector<int>(MB+1,0));
depth=vector<int>(N,0); dfs(ROOT,ROOT);
}
LCA() {}
int kth(int node,int k) {
if(k>depth[node]) return -1;
int bit=0;
while(k) {
if(k&1) node=up[node][bit];
k>>=1; bit++;
}
return node;
}
int getlca(int u, int v) {
if(depth[u]<depth[v]) swap(u, v);
for(int bit=MB;bit>=0;bit--)
if(depth[u]-(1<<bit)>=depth[v])
u=up[u][bit];
if(u==v) return u;
for(int bit=MB;bit>=0;bit--)
if(up[u][bit]!=up[v][bit])
u=up[u][bit],v=up[v][bit];
return up[u][0];
}
int dist(int u,int v) {
return depth[u]+depth[v]-2*depth[getlca(u,v)];
}
};
class dsu{
public:
int N;
vector<int> par,size;
vector<array<int,2>> dd;
LCA lc;
dsu(int n, LCA lc) {
this->lc=lc;
N=n;
par=size=vector<int>(N,1);
dd=vector<array<int,2>>(N);
for(int i=0;i<N;i++) {
par[i]=i;
dd[i]={i,i};
}
}
int find(int u) {
return par[u]==u?u:par[u]=find(par[u]);
}
int join(int u,int v) {
u=find(u),v=find(v);
if(u==v) return 0;
if(size[u]<size[v]) swap(u,v);
par[v]=u;
size[u]+=size[v];
vector<int> diam={dd[u][0],dd[u][1],dd[v][0],dd[v][1]};
int d1=0,d2=0;
for(int i=0;i<4;i++) {
for(int j=i+1;j<4;j++) {
if(lc.dist(diam[i],diam[j]) > lc.dist(diam[d1],diam[d2])) {
d1=i; d2=j;
}
}
}
dd[u]={diam[d1],diam[d2]};
return 1;
}
};
void solve() {
int n,m;
cin>>n>>m;
/*
for every node suppose we have:
dist from root
now let earlier diameter be D1...D2
either this will remain unchanged
or D1..cur
or D2..cur
check all these using lca?
*/
vector<vector<int>> e(n),qq(m);
for(int i=0;i<m;i++) {
int t; cin>>t;
if(t==1) {
int u,v;
cin>>u>>v;
u--;v--;
e[u].push_back(v);
e[v].push_back(u);
qq[i]={1,u,v};
} else {
int u; cin>>u; u--;
qq[i]={2,u};
}
}
// dbg(qq); exit(0);
dbg(qq);
LCA lca(e);
dsu d(n, lca);
for(int i=0;i<m;i++) {
int t=qq[i][0];
if(t==1) {
d.join(qq[i][1],qq[i][2]);
continue;
}
int node=qq[i][1];
int leader=d.find(node);
int d1=d.dd[leader][0],d2=d.dd[leader][1];
int dist1=lca.dist(node,d1), dist2=lca.dist(node,d2);
cout<<max(dist2,dist1)<<" "<<(dist1>dist2?d1+1:d2+1)<<"\n";
}
cout<<"\n";
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t;
cin>>t;
while(t--) {
solve();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNpZmRlZiBBTkkKI2luY2x1ZGUgIkQ6L0RVU1RCSU4vbG9jYWxfaW5jLmgiCiNlbHNlCiNkZWZpbmUgZGJnKC4uLikgMAojZW5kaWYKCmNsYXNzIExDQXsKcHVibGljOgoJaW50IE1CPTIwLE4sUk9PVDsKCXZlY3Rvcjx2ZWN0b3I8aW50Pj4gdXAsZTsKCXZlY3RvcjxpbnQ+IGRlcHRoOwoJdm9pZCBkZnMoaW50IGN1cixpbnQgcGFyKSB7CgkJdXBbY3VyXVswXT1wYXI7CgkJZm9yKGludCBiaXQ9MTtiaXQ8PU1CO2JpdCsrKSB7CgkJCXVwW2N1cl1bYml0XT11cFt1cFtjdXJdW2JpdC0xXV1bYml0LTFdOwoJCX0KCQlmb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