//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long //#define L __int128 typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,x) for(int i=0;i bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b using vc=vector; template ostream& operator<<(ostream& os,const pair& p){ return os<<"{"< ostream& operator<<(ostream& os,const vc& v){ os<<"{"; for(auto e:v)os<> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; //unordered_set -> dtype, null_type //unordered_map -> dtype(key), dtype(value) using namespace __gnu_pbds; template using hash_table=gp_hash_table; template void g(T &a){ cin >> a; } template void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 998244353; //const ll mod = 1000000007; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); template void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } #define _sz 1 ll F[_sz],R[_sz]; void make(){ F[0] = 1; for(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod; R[_sz-1] = modpow(F[_sz-1], mod-2); for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod; } ll C(int a,int b){ if(b < 0 || a < b) return 0; return F[a]*R[b]%mod*R[a-b]%mod; } #define szz 200005 int n; vectoredge[szz]; int up[20][szz],dep[szz]; int cnt[szz]; int hld_cmp[szz],hld_num[szz],hld_sz[szz]; int in[szz],out[szz],rev[szz],nxt; struct dbling{ bool ready = 0; void dfs(int v,int u,int d){ up[0][v] = u; dep[v] = d; cnt[v] = 1; P p = mp(0, 0); rep(i,edge[v].size()){ if(edge[v][i] == u) continue; dfs(edge[v][i], v, d+1); p = max(p,mp(cnt[edge[v][i]],i)); cnt[v] += cnt[edge[v][i]]; } swap(edge[v][0],edge[v][p.sc]); } void make_hld(int v,int u,int up,int id){ hld_cmp[v] = up; hld_num[v] = id; hld_sz[up] = max(hld_sz[up],id); in[v]=nxt; rev[nxt] = v; nxt++; for(int i=0;i dep[b]) swap(a,b); int d = dep[b]-dep[a]; rep(i,20){ if(((d>>i)&1)) b = up[i][b]; } if(a == b) return a; for(int i=19;i>=0;i--){ if(up[i][a] != up[i][b]){ a = up[i][a]; b = up[i][b]; } } return up[0][a]; } int dist(int a,int b){ int c = get(a,b); return dep[a]+dep[b]-2*dep[c]; } int dist(int a,int b,int c){ //assuming c is lca of (a,b) return dep[a]+dep[b]-2*dep[c]; } int go_up(int v,int a){ if(dep[v] < a) return -1; rep(i,20) if(((a>>i)&1)) v = up[i][v]; return v; } //a ---- b //と並べたときのx番目 (1-indexed) //存在しない場合は-1を返す //verified : opencup warsaw i int v_on_path(int a,int b,int x){ int c = get(a,b); int d = dist(a,b,c); if(x <= 0 || x > d+1) return -1; if(x <= dep[a]-dep[c]+1){ return go_up(a, x-1); } else{ x = d+2-x; return go_up(b, x-1); } } }kaede; int ran[szz], beg[szz], upp[szz], val[szz]; struct node{ node *l, *r, *p; int id; node(int i) : l(0), r(0), p(0), id(i) {} }; inline bool is_root(node *n){ return n -> p == NULL || n -> p -> l != n && n -> p -> r != n; } inline bool left(node *n){ return n == n -> p -> l; } //遅延評価 inline void push(node *n){ } //値の再計算 inline void update(node *n){ //最初に遅延評価 push(n); } inline void connect(node *n, node *p, bool l){ (l ? p -> l : p -> r) = n; if(n) n -> p = p; } //rotateが呼ばれる前には関与しているノードの遅延評価をする必要がある inline void rotate(node *n){ node *p = n -> p, *g = p -> p; bool l = left(n); connect(l ? n -> r : n -> l, p, l); if(!is_root(p)) connect(n, g, left(p)); else n -> p = g; connect(p, n, !l); update(p), update(n); } inline void splay(node *n){ while(!is_root(n)){ node *p = n -> p, *g = p -> p; //関与する頂点群の遅延評価をする if(!is_root(p)) push(g); push(p), push(n); if(!is_root(p)) rotate(left(n) ^ left(p) ? n : p); rotate(n); } //最後に遅延評価 push(n); } inline node* find_root(node *n){ if(n == NULL) return NULL; while(n -> l){ //関与するノードの遅延評価を行う push(n); n = n -> l; } return n; } //返り値はnじゃないよ inline node* expose(node *n){ node *last = NULL; for(node *m = n; m; m = m -> p){ splay(m); m -> r = last; last = m; } splay(n); return last; } void link(node *m, node *n){ expose(m), expose(n); m -> p = n; //mの部分木分増やす } void cut(node *n){ expose(n); //nの部分木分減らす n -> l -> p = NULL; n -> l = NULL; } //非連結なら-1 int LCA(node *a,node *b){ expose(a); node *ret = expose(b); if(a->p == (node*)NULL) return -1; else return ret->id; } const int MAXN = 200005; node *V[MAXN]; template struct segtree{ int sz; vectorseg; const F f; const T e; segtree(int n, const F f, const T e): f(f), e(e) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2*sz, e); } void init(int n){ sz = 1; while(sz < n) sz <<= 1; seg.assign(2*sz, e); } //mrg...apply f or not void update(int a, T p, bool mrg){ a += sz-1; if(mrg) seg[a] = f(seg[a], p); else seg[a] = p; while(a){ a = (a-1)/2; seg[a] = f(seg[a*2+1], seg[a*2+2]); } } void make(vectora){ rep(i, a.size()) seg[i+sz-1] = a[i]; for(int i=sz-2;i>=0;i--) seg[i] = f(seg[i*2+1], seg[i*2+2]); } T query(int a, int b){ a += sz-1, b += sz-1; T L = e, R = e; while(a <= b){ if((a&1) == 0) { L = f(L, seg[a++]); } if((b&1) == 1) { R = f(seg[b--], R); } if(a > b) break; a = (a-1) >> 1; b = (b-1) >> 1; } return f(L, R); } int find_left(int a, int b, int k, int l, int r, T v){ if(r < a || b < l) return INF; if(a <= l && r <= b){ if(f(seg[k], v) == seg[k]){ while(k < sz-1){ if(f(seg[k*2+1], v) == seg[k*2+1]) k = k*2+1; else k = k*2+2; } return k - (sz-1); } return INF; } int ret = find_left(a, b, k*2+1, l, (l+r)/2, v); if(ret != INF) return ret; return find_left(a, b, k*2+2, 1+(l+r)/2, r, v); } //NOT VERIFIED int find_right(int a, int b, int k, int l, int r, T v){ if(r < a || b < l) return -INF; if(a <= l && r <= b){ if(f(seg[k], v) == seg[k]){ while(k < sz-1){ if(f(seg[k*2+2], v) == seg[k*2+2]) k = k*2+2; else k = k*2+1; } return k - (sz-1); } return -INF; } int ret = find_right(a, b, k*2+2, 1+(l+r)/2, r, v); if(ret != -INF) return ret; return find_right(a, b, k*2+1, l, (l+r)/2, v); } //[a, b]でf(*, v)=*となる最小の*を返す 無ければINF int find_left(int a, int b, T v){ return find_left(a, b, 0, 0, sz-1, v); } //NOT VERIFIED //[a, b]でf(*, v)=*となる最大の*を返す 無ければ-INF int find_right(int a, int b, T v){ return find_right(a, b, 0, 0, sz-1, v); } }; auto f = [](P a, P b){ return max(a, b); }; P e = mp(-1e18, -1e18); segtreeseg((1<<18), f, e); P get(int v, int ch){ P ret = e; if(ch != up[0][v] and v != 1) chmax(ret, mp(val[up[0][v]], up[0][v])); if(1<=ch and ch<=n and beg[v]<=ran[ch] and ran[ch]<=upp[v]){ chmax(ret, seg.query(beg[v], ran[ch]-1)); chmax(ret, seg.query(ran[ch]+1, upp[v])); } else chmax(ret, seg.query(beg[v], upp[v])); if(ret.a<0) return mp(v, v); return ret; } void solve(){ cin>>n; rep(i, n-1){ int a, b; cin >> a >> b; edge[a].pb(b); edge[b].pb(a); } kaede.prepare(); vcused(n+1, 0); vcdist(n+1, INF); vcarr(n+1); queueque; que.push(1); dist[1] = 0; int idx = 1; while(!que.empty()){ auto q = que.front(); que.pop(); if(used[q]) continue; used[q] = 1; arr[idx] = q; ran[q] = idx++; for(auto a:edge[q]){ chmin(dist[a], dist[q]+1); if(!used[a]) que.push(a); } } repn(i, n) val[i] = i; repn(i, n) V[i] = new node(i); repn(i, n){ beg[i] = INF, upp[i] = -INF; int cnt = 0; for(auto a:edge[i]){ if(dist[i]+1 == dist[a]){ chmin(beg[i], ran[a]); chmax(upp[i], ran[a]); cnt++; } } if(beg[i] <= upp[i]){ assert(upp[i]-beg[i]+1==cnt); P p = mp(-1, -1); for(int j=beg[i];j<=upp[i];j++){ chmax(p, mp(val[arr[j]], arr[j])); } link(V[i], V[p.b]); } } /* repn(i, n){ expose(V[i]); cout << find_root(V[i])->id << endl; }*/ for(int i=1;i<=n;i++) seg.update(ran[i], mp(val[i], i), false); vc>chain(n+1, vc()); repn(i, n) chain[i].resize(hld_sz[i]+1, 0); repn(i, n) chain[hld_cmp[i]][hld_num[i]] = i; using st = segtree; auto segh = vc(); segh.reserve(n+1); rep(i, n+1){ segh.pb(st((int)(hld_sz[i]+2) * 2, f, e)); } P reve = mp(1e18, 1e18); repn(i, n){ if(hld_sz[i] <= 2) continue; for(int j=2;j> q; while(q--){ int u, v; cin >> u >> v; //cout<({u, v})){ int r = hld_num[see]; int cc = hld_cmp[see]; { int U = up[0][see]; if(U >= 1){ int nxtt=-1; if(cc == hld_cmp[U]) nxtt = see; else if(hld_num[U] != hld_sz[hld_cmp[U]]){ nxtt = chain[hld_cmp[U]][hld_num[U]+1]; } auto xx = get(U, nxtt); bool f = xx.b == up[0][U]; int nc = hld_cmp[U]; segh[nc].update(hld_num[U], (!f?reve:e), false); } } if(r+1 < hld_sz[cc]){ auto xx = get(chain[cc][r+1], chain[cc][r+2]); bool f = xx.b == chain[cc][r]; assert(chain[cc][r] == see); segh[cc].update(r+1, (!f?reve:e), false); } } for(auto pr:vc({up[0][u], up[0][v]})){ if(pr < 1) continue; if(beg[pr] > upp[pr]) continue; auto k = seg.query(beg[pr], upp[pr]); cut(V[pr]); link(V[pr], V[k.b]); } //for(int i=1;i<=n;i++) o(segh[hld_cmp[i]].query(hld_num[i], hld_num[i])); //return; //u -(up)-> w auto xx = get(u, -1); //o(xx); if(xx.b != up[0][u]){ expose(V[xx.b]); pre = find_root(V[xx.b])->id; o(pre); } else{ int prevv = u; int cur = xx.b; while(cur != 1){ //cout<id; o(pre); goto nxt; } prevv = cur; cur = yy.b; } else{ assert(2 <= zan and zan < len and cc == hld_cmp[prevv]); auto x = segh[cc].find_right(2, zan, reve); if(x < 0){ prevv = chain[cc][2]; cur = chain[cc][1]; } else{ prevv = chain[cc][x+1]; cur = chain[cc][x]; //cout<id; o(pre); goto nxt; } } } nxt:; if(cur != 1) continue; //cout<id; o(pre); } } } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<> t; while(t--) solve(); }