LCA
#include<stdio.h>
#include<string.h>
#include<vector>
#include<cmath>
using namespace std;
vector<int> c[10005];
int n,cnt,first[10005],dfs_val[20100],dfs_deep[20100];
int dp[20100][16];
void dfs(int root,int deep)
{
dfs_deep[++cnt]=deep;
dfs_val[cnt]=root;
first[root]=cnt;
for(int i=0;i<c[root].size();i++)
{
dfs(c[root][i],deep+1);
dfs_deep[++cnt]=deep;
dfs_val[cnt]=root;
}
}
void st()
{
int i,j;
for(i=1;i<=cnt;i++) dp[i][0]=i;
for(j=1;j<=(int)(log(1.0*cnt)/log(2.0));j++)
{
for(i=1;i<=cnt+1-(1<<j);i++)
{
int p=i+(1<<(j-1));
if(dfs_deep[dp[i][j-1]]>dfs_deep[dp[p][j-1]]) dp[i][j]=dp[p][j-1];
else dp[i][j]=dp[i][j-1];
}
}
}
int rmq(int l,int r)
{
l=first[l],r=first[r];
if(l>r) swap(l,r);
int k=(int)(log(1.0*(r-l+1))/log(2.0));
if(dfs_deep[dp[l][k]]<dfs_deep[dp[r+1-(1<<k)][k]]) return dp[l][k];
else return dp[r+1-(1<<k)][k];
}
int main()
{
int i,t,x,y,root;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++) c[i].clear();
scanf("%d%d",&x,&y);
c[x].push_back(y);
root=x;
for(i=2;i<n;i++)
{
scanf("%d%d",&x,&y);
c[x].push_back(y);
if(y==root) root=x;
}
cnt=0;
memset(first,0,sizeof(first));
dfs(root,0);
st();
scanf("%d%d",&x,&y);
printf("%d\n",dfs_val[rmq(x,y)]);
}
}