#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=12000;
struct edge
{
int t;
int next;
};
int V;
int p[maxn];
edge G[maxn];
int l;
int fath[maxn];///父节点
int dep[maxn];///深度
void init()
{
memset(p,-1,sizeof(p));///初始化
memset(fath,-1,sizeof(fath));///按照并查集的观点来说应该是father[i]=i,但是这里的数是精准的 so...no
l=0;
}
void addedge(int u,int t,int l)
{
G[l].t=t;
G[l].next=p[u];
p[u]=l;
fath[t]=u;
}
void LCA(int u,int deep)
{
dep[u]=deep;
for(int i=p[u];i!=-1;i=G[i].next)
{
LCA(G[i].t,deep+1);
}
}
int findLCA(int u,int t)
{
if(dep[u]<dep[t]) swap(u,t);
while(dep[u]>dep[t])
{
u=fath[u];
}
while(u!=t)
{
u=fath[u];
t=fath[t];
}
return t;
}
int main()
{
int ci;
scanf("%d",&ci);
while(ci--&&scanf("%d",&V)==1)
{
init();
for(int i=1;i<V;i++)
{
int u,t;
scanf("%d%d",&u,&t);
addedge(u,t,l++);
}
for(int i=1;i<=V;i++)
{
if(fath[i]==-1)///从根节点开始搜索关于深度的东西
{
LCA(i,1);
break;
}
}
int st,ed;
scanf("%d%d",&st,&ed);
int tmp=findLCA(st,ed);
printf("%d\n",tmp);
}
return 0;
}