#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <queue>
using namespace std;
const int maxn=1e5;
typedef long long int LL;
const int INF=1e9;
struct Node
{
int value;
int next;
int value2;
int id;
}edge[maxn*4+5],a[maxn+5];
int head[maxn+5];
int tot;
void add(int x,int y,int z)
{
edge[tot].value=y;
edge[tot].value2=x;
edge[tot].next=head[x];
edge[tot].id=z;
head[x]=tot++;
}
struct node
{
int pre;
int next;
int id;
int num;
int num2;
int pos;
node(){};
node(int pre,int next,int id,int num,int num2,int pos)
{
this->pre=pre;
this->next=next;
this->id=id;
this->num=num;
this->num2=num2;
this->pos=pos;
}
friend bool operator <(node a,node b)
{
return a.num>b.num;
}
};
int d[maxn+5];//站点数19958
int v[maxn+5];//换乘次数
int n,m;
int route[maxn+5];
int vis[maxn+5];
int Dijkstra(int s,int e)
{
priority_queue<node> q;
int res1=INF;
int res2=INF;
int ans=-1;
memset(vis,0,sizeof(vis));
for(int i=0;i<=tot;i++)
d[i]=v[i]=INF;
for(int i=head[s];i!=-1;i=edge[i].next)
{
d[i]=1;v[i]=0;
route[i]=-1;
vis[i]=1;
q.push(node(s,edge[i].value,edge[i].id,d[i],v[i],i));
}
while(!q.empty())
{
node term=q.top();
q.pop();
vis[term.pos]=1;
if(term.next==e)
{
if(res1>term.num)
{
ans=term.pos;
res1=term.num;
res2=term.num2;
}
else if(res1==term.num)
{
if(res2>term.num2)
{
res2=term.num2;
ans=term.pos;
}
}
}
for(int i=head[term.next];i!=-1;i=edge[i].next)
{
int x=edge[i].value;
if(vis[i]) continue;
if(d[i]>term.num+1)
{
d[i]=term.num+1;
if(term.id!=edge[i].id)
v[i]=term.num2+1;
else
v[i]=term.num2;
route[i]=term.pos;
q.push(node(term.next,edge[i].value,edge[i].id,d[i],v[i],i));
}
else if(d[i]==term.num+1)
{
int xx;
if(term.id!=edge[i].id)
xx=term.num2+1;
else
xx=term.num2;
if(v[i]>xx)
{
v[i]=xx;
route[i]=term.pos;
q.push(node(term.next,edge[i].value,edge[i].id,d[i],v[i],i));
}
}
}
}
return ans;
}
int main()
{
scanf("%d",&n);
int k;
memset(head,-1,sizeof(head));
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
int x,y=-1;
for(int j=1;j<=k;j++)
{
scanf("%d",&x);
if(y==-1)
{
y=x;
continue;
}
else
{
add(x,y,i);
add(y,x,i);
}
y=x;
}
}
scanf("%d",&m);
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
//if(x==y)
//{printf("0\n");continue;}
int res=Dijkstra(x, y);
if(res==-1)
{printf("Sorry, no line is available.\n");continue;}
printf("%d\n",d[res]);
int cot=0;
while(res!=-1)
{
a[++cot]=edge[res];
res=route[res];
}
int p=-1;
int tag=0;
while(cot>=2)
{
if(a[cot].id!=p)
{
if(p!=-1)
printf(" to %04d.\n",a[cot].value2);
printf("Go by the line of company #%d from %04d",a[cot].id,a[cot].value2);
p=a[cot].id;
}
cot--;
tag=1;
}
if(a[cot].id!=p)
{
if(tag)
printf(" to %04d.\n",a[cot].value2);
printf("Go by the line of company #%d from %04d to %04d.\n",a[cot].id,a[cot].value2,a[cot].value);
}
else
printf(" to %04d.\n",a[cot].value);
}
return 0;
}