#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
vector<int> E[maxn];
int dep[maxn],f[maxn][22];
void dfs(int fa, int x)
{
f[x][0]=fa;dep[x]=dep[fa]+1;
for (int k = 1; k <= 21; k++)
f[x][k] = f[f[x][k - 1]][k - 1];
for (int i = 0; i < E[x].size(); i++)
{
int now = E[x][i];
if (now == fa)
continue;
dfs(x,now);
}
}
int lca(int x,int y)
{
if(dep[x]>dep[y])
swap(x,y);
for(int i=21;i>=0;i--)
{
if(dep[f[y][i]]>=dep[x])
y=f[y][i];
if(x==y)
return x;
}
for(int i=21;i>=0;i--)
{
if(f[y][i]!=f[x][i])
x=f[x][i],y=f[y][i];
}
return f[x][0];
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n, m, s;
cin >> n >> m >> s;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
E[x].push_back(y);
E[y].push_back(x);
}
dep[s]=0;
f[s][0]=s;
dfs(s,s);
while (m--)
{
int x, y;
cin >> x >> y;
cout<<lca(x,y)<<'\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 4;
vector<int> V[maxn];
int dep[maxn];
int first[maxn];
int id[maxn];
int tot = 1;
int n;
int ST[maxn][30];
void dfs(int fa, int x,int deep)
{
first[x] = tot;
id[tot] = x;
dep[tot] = deep;
tot++;
for (int i = 0; i < V[x].size(); i++)
{
int v = V[x][i];
if (v != fa)
{
dfs(x, v,deep+1);
id[tot] = x;dep[tot]=deep;
tot++;
}
}
}
void init_ST()
{
for (int i = 1; i < 2 * n; i++)
ST[i][0] = i;
for (int j = 1; (1 << j) < 2 * n; j++)
for (int i = 1; i+(1<<j) -1< 2 * n; i++)
{
int a = ST[i][j - 1], b = ST[i + (1 << j-1) ][j - 1];
ST[i][j] = dep[a] <= dep[b] ? a : b;
}
}
int RMQ(int x, int y)
{
int l = first[x], r = first[y];
if (l > r)
swap(l, r);
if(l==r) return id[l];
int dis = log2(r - l + 1);
int a = ST[l][dis], b = ST[r - (1 << dis) + 1][dis];
return id[dep[a] <= dep[b] ? a : b];
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin >> t;
for (int k = 1; k <= t; k++)
{
cout << "Case " << k << ":\n";
tot = 1;
cin >> n;
for (int i = 0; i <= n; i++)
V[i].clear();
for (int i = 1; i <= n; i++)
{
int m;
cin >> m;
while (m--)
{
int x;
cin >> x;
V[i].push_back(x);
V[x].push_back(i);
}
}
dfs(1, 1,0);
init_ST();
int q;
cin >> q;
while (q--)
{
int x, y;
cin >> x >> y;
if (x == y)
cout << x << '\n';
else
cout << RMQ(x, y) << '\n';
}
}
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
int n;
vector<int> V[maxn];
struct node
{
int y,idx;
node(int a=0,int b=0)
{
y=a,idx=b;
}
};
vector<node> query[maxn];
int ans[maxn];
int fa[maxn];
bool vis[maxn];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void tarjan(int x)
{
fa[x]=x;vis[x]=1;
for(int i=0;i<V[x].size();i++)
{
int v=V[x][i];
if(!vis[v])
{
tarjan(v);
fa[v]=x;
}
}
for(int i=0;i<query[x].size();i++)
{
int y=query[x][i].y,idx=query[x][i].idx;
if(vis[y])
ans[idx]=find(y);
}
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
for(int k=1;k<=t;k++)
{
cout<<"Case "<<k<<":\n";
cin>>n;
for(int i=0;i<=n;i++)
V[i].clear(),query[i].clear(),vis[i]=0;
for(int i=1;i<=n;i++)
{
int m;
cin>>m;
while (m--)
{
int x;
cin>>x;
V[i].push_back(x);
V[x].push_back(i);
}
}
int Q;
cin>>Q;
for(int i=1;i<=Q;i++)
{
int x,y;
cin>>x>>y;
query[x].push_back(node(y,i));
query[y].push_back(node(x,i));
}
tarjan(1);
for(int i=1;i<=Q;i++)
cout<<ans[i]<<'\n';
}
return 0;
}