#include <algorithm> #include <iostream> #include <cstring> #include <iomanip> #include <cstdlib> #include <climits> #include <cstdio> #include <bitset> #include <string> #include <vector> #include <cctype> #include <stack> #include <queue> #include <deque> #include <cmath> #include <map> #include <set>
#define I inline #define fi first #define se second #define pb push_back #define MP make_pair #define PII pair<int , int> #define PSL pair<string , long long> #define PLL pair<long long , long long> #define all(x) (x).begin() , (x).end() #define copy(a , b) memcpy(a , b , sizeof(b)) #define clean(a , b) memset(a , b , sizeof(a)) #define rep(i , l , r) for (int i = (l); i <= (r); i ++) #define per(i , r , l) for (int i = (r); i >= (l); i --) #define PE(i , x) for(int i = head[x]; i; i = edge[i].last) #define DEBUG(x) std :: cerr << #x << '=' << x << std :: endl
typedef long long ll; typedef unsigned long long ull;
template <class T> inline void read(T &x) { char c = getchar(), f = 0; x = 0; while (!isdigit(c)) f = (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x = f ? -x : x; }
using namespace std;
const int N = 10001; const int M = 100001; const int HA = 998244353;
int n;
struct Edge{ int from , to , last; }edge[M << 1];
int edge_num; int head[M << 1];
I void add_edge(int from , int to){ edge[++ edge_num] = (Edge){from , to , head[from]}; head[from] = edge_num; edge[++ edge_num] = (Edge){to , from , head[to]}; head[to] = edge_num; }
int fa[M] , size[M] , cnt = 0x3f3f3f3f;
int root[3];
void dfs1(int x , int f){ fa[x] = f; size[x] = 1; int sz = 0; PE(i , x){ int to = edge[i].to; if(to == f) continue; dfs1(to , x); size[x] += size[to]; sz = max(sz , size[to]); } sz = max(sz , n - size[x]); if(sz < cnt){ cnt = sz; root[1] = x , root[2] = 0; } else if(sz == cnt){ root[2] = x; } }
int leaf;
void dfs2(int x , int f){ if(size[x] == 1){ leaf = x; return; } PE(i , x){ int to = edge[i].to; if(to == f) continue; dfs2(to , x); } }
int main() { #ifdef LOCAL freopen("try.in" , "r" , stdin); freopen("try1.out" , "w" , stdout); #endif int t; read(t); while(t --){ edge_num = 0; rep(i , 1 , n){ head[i] = fa[i] = size[i] = 0; } cnt = 0x3f3f3f3f; root[1] = root[2] = 0; read(n); rep(i , 1 , n - 1){ int u , v; read(u) , read(v); add_edge(u , v); } dfs1(1 , 0); if(root[1] != 0 && root[2] == 0){ printf("%d %d\n%d %d\n" , edge[1].from , edge[1].to , edge[1].to , edge[1].from); continue; } if(fa[root[1]] != root[2]) swap(root[1] , root[2]); dfs2(root[1] , root[2]); printf("%d %d\n%d %d\n" , leaf , fa[leaf] , leaf , root[2]); }
return 0; }
|