#include<bits/stdc++.h>
using namespace std;
#define in(n) scanf("%d",&n)
#define pb push_back
#define N 1005
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
vector<vector<int> > ad(N);
int po[12],ar[4*N],lev[N],sp[2*N][11],vp[2*N],fist[N];
int c;
void dfs(int s)
{
fist[s]=c,vp[c]=s,c++;
int i,x=ad[s].size();
for(i=0;i<x;i++)
lev[ad[s][i]]=lev[s]+1,dfs(ad[s][i]),vp[c]=s,c++;
return ;
}
void up(int j,int n)
{
sp[j][0]=vp[j];
for(int k=1;k<12;k++)
if(j+po[k]<=n)sp[j][k]=lev[sp[j][k-1]]<lev[sp[j+po[k-1]][k-1]]?sp[j][k-1]:sp[j+po[k-1]][k-1];
return ;
}
int query(int a,int b)
{
int y,x=b-a+1;
y=ar[x],x-=po[y];
return lev[sp[a][y]]<lev[sp[a+x][y]]?sp[a][y]:sp[a+x][y];
}
int main()
{
int k,i,t,w,n,x,a,b,q,j;
ar[1]=0,k=1,po[0]=1;
while(k<2*N)
{
for(i=k+1;i<2*k;i++)
ar[i]=ar[k];
ar[2*k]=1+ar[k],k*=2;
}
for(i=1;i<12;i++)
po[i]=2*po[i-1];
in(t);
for(w=1;w<=t;w++)
{
in(n),c=0;
for(i=0;i<n;i++)
{
in(x);
ad[i].resize(x);
for(j=0;j<x;j++)
in(a),ad[i][j]=a-1;
}
lev[0]=0,dfs(0);
for(int i=c-1;i>=0;i--)
up(i,c);
printf("Case %d:\n",w);
in(q);
while(q--)
{
in(a),in(b);
x=1+query(min(fist[a-1],fist[b-1]),max(fist[a-1],fist[b-1]));
out(x);
}
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbihuKSBzY2FuZigiJWQiLCZuKQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIE4gMTAwNQojZGVmaW5lIHByKG4pIHByaW50ZigiJWQgIixuKQojZGVmaW5lIG91dChuKSBwcmludGYoIiVkXG4iLG4pCnZlY3Rvcjx2ZWN0b3I8aW50PiA+IGFkKE4pOwppbnQgcG9bMTJdLGFyWzQqTl0sbGV2W05dLHNwWzIqTl1bMTFdLHZwWzIqTl0sZmlzdFtOXTsKaW50IGM7CnZvaWQgZGZzKGludCBzKQp7CiAgICBmaXN0W3NdPWMsdnBbY109cyxjKys7CiAgICBpbnQgaSx4PWFkW3NdLnNpemUoKTsKICAgIGZvcihpPTA7aTx4O2krKykKICAgICAgICBsZXZbYWRbc11baV1dPWxldltzXSsxLGRmcyhhZFtzXVtpXSksdnBbY109cyxjKys7CiAgICByZXR1cm4gOwp9CnZvaWQgdXAoaW50IGosaW50IG4pCnsKICAgIHNwW2pdWzBdPXZwW2pdOwogICAgZm9yKGludCBrPTE7azwxMjtrKyspCiAgICAgICAgaWYoaitwb1trXTw9bilzcFtqXVtrXT1sZXZbc3Bbal1bay0xXV08bGV2W3NwW2orcG9bay0xXV1bay0xXV0/c3Bbal1bay0xXTpzcFtqK3BvW2stMV1dW2stMV07CiAgICByZXR1cm4gOwp9CmludCBxdWVyeShpbnQgYSxpbnQgYikKewogICAgaW50IHkseD1iLWErMTsKICAgIHk9YXJbeF0seC09cG9beV07CiAgICByZXR1cm4gbGV2W3NwW2FdW3ldXTxsZXZbc3BbYSt4XVt5XV0/c3BbYV1beV06c3BbYSt4XVt5XTsKfQppbnQgbWFpbigpCnsKICAgIGludCBrLGksdCx3LG4seCxhLGIscSxqOwogICAgYXJbMV09MCxrPTEscG9bMF09MTsKICAgIHdoaWxlKGs8MipOKQogICAgewogICAgICAgIGZvcihpPWsrMTtpPDIqaztpKyspCiAgICAgICAgICAgIGFyW2ldPWFyW2tdOwogICAgICAgIGFyWzIqa109MSthcltrXSxrKj0yOwogICAgfQogICAgZm9yKGk9MTtpPDEyO2krKykKICAgICAgICBwb1tpXT0yKnBvW2ktMV07CiAgICBpbih0KTsKICAgIGZvcih3PTE7dzw9dDt3KyspCiAgICB7CiAgICAgICAgaW4obiksYz0wOwogICAgICAgIGZvcihpPTA7aTxuO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGluKHgpOwogICAgICAgICAgICBhZFtpXS5yZXNpemUoeCk7CiAgICAgICAgICAgIGZvcihqPTA7ajx4O2orKykKICAgICAgICAgICAgICAgIGluKGEpLGFkW2ldW2pdPWEtMTsKICAgICAgICB9CiAgICAgICAgbGV2WzBdPTAsZGZzKDApOwogICAgICAgIGZvcihpbnQgaT1jLTE7aT49MDtpLS0pCiAgICAgICAgICAgIHVwKGksYyk7CiAgICAgICAgcHJpbnRmKCJDYXNlICVkOlxuIix3KTsKICAgICAgICBpbihxKTsKICAgICAgICB3aGlsZShxLS0pCiAgICAgICAgewogICAgICAgICAgICBpbihhKSxpbihiKTsKICAgICAgICAgICAgeD0xK3F1ZXJ5KG1pbihmaXN0W2EtMV0sZmlzdFtiLTFdKSxtYXgoZmlzdFthLTFdLGZpc3RbYi0xXSkpOwogICAgICAgICAgICBvdXQoeCk7CiAgICAgICAgfQogICAgfQp9Cg==